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?
class Solution { public: void rotate(vector<vector<int> > &matrix) { int temp; for(int i = 0; i < matrix.size()/2; i++) { for(int j = i; j < matrix[0].size()-i-1; j++) { temp = matrix[i][j]; matrix[i][j] = matrix[matrix[0].size()-1-j][i]; matrix[matrix[0].size()-1-j][i] = matrix[matrix.size()-1-i][matrix.size()-1-j]; matrix[matrix.size()-1-i][matrix.size()-1-j] = matrix[j][matrix.size()-1-i]; matrix[j][matrix.size()-1-i] = temp; } } } };
时间: 2024-10-10 00:11:19