-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
Question:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Solution:
思路:将正方形分解为一个套一个的正方形,从矩阵的最外层(一个正方形),开始变换,一直循环到到中间的四个元素组成的正方形。
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
vector<vector<int>> result;
int n = matrix.size();
int i , j ;
if(n == 0 ) return ;
if(n == 1 ) return ;
for(i = 0 ; i < n/2; i++)
for( j = i ; j < n - i - 1; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[n-1-j][i];
matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
matrix[j][n-1-i] = temp;
}
}
};Metadata
Metadata
Assignees
Labels
No labels