Rotate Image
LeetCode #48
Description:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
Example:
Note
Idea:
先按照行或者列翻转,再transpose。
Code:
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int i=0;
int j=matrix.size()-1;
// 先翻折
while(i<j){
swap(matrix[i], matrix[j]);
i++;
j--;
}
// 再transpose
for(i=1; i<matrix.size(); ++i){
for(j=0; j<i; ++j){
swap(matrix[i][j], matrix[j][i]);
}
}
}
};