Skip to content

Commit

Permalink
LeetCode/48.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
userr2232 committed Apr 10, 2022
1 parent fdfb4d4 commit fafc805
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
30 changes: 30 additions & 0 deletions LeetCode/48.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
void rotate(const int& start, const int& end, vector<vector<int>>& matrix) {
const int diff = end-start;
for(int m = 0; m < diff; m++) {
int i{start}, j{start+m};
int di{0}, dj{-1};
for(int k = 0; k < 3; ++k) {
int next_di{-dj}, next_dj{di};
int next_i{i+di*diff}, next_j{j+dj*diff};
int excess_i{next_i < start ? next_i-start : next_i > end ? next_i-end : 0};
int excess_j{next_j < start ? next_j-start : next_j > end ? next_j-end : 0};
next_i -= excess_i, next_j -= excess_j;
next_i += next_di * abs(excess_j), next_j += next_dj * abs(excess_i);
swap(matrix[i][j], matrix[next_i][next_j]);
di = next_di, dj = next_dj;
i = next_i, j = next_j;
}
}
}

public:
void rotate(vector<vector<int>>& matrix) {
const int n = matrix.size();
int l{0}, r{n-1};
while(l < r) {
rotate(l, r, matrix);
l++, r--;
}
}
};

0 comments on commit fafc805

Please sign in to comment.