Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkingtospace/46ff90fa43f7051dc529 to your computer and use it in GitHub Desktop.
Save walkingtospace/46ff90fa43f7051dc529 to your computer and use it in GitHub Desktop.
rotate-image
https://oj.leetcode.com/problems/rotate-image/
/*
tese case
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
in-place
단순한 규칙을 찾고 loop를 2D로 구현하여야 함.
*/
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int size = matrix.size();
int halfSize = size/2;
for(int i=0; i<halfSize ; ++i) {
for(int j=i ; j<size-1-i ; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix [size-1-j][i];
matrix[size-1-j][i] = matrix[size-1-i][size-1-j];
matrix[size-1-i][size-1-j] = matrix[j][size-1-i];
matrix[j][size-1-i] = temp;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment