[LeetCode]Rotate Image
Rotate Image
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?
每次转换四个点。
1 class Solution { 2 public: 3 void rotate(vector<vector<int>>& matrix) { 4 if(matrix.size()<=1) return; 5 int n = matrix.size(); 6 for(int i=0;i<n;i++) 7 { 8 for(int j=i;j<n-1-i;j++) 9 { 10 int temp = matrix[i][j]; 11 matrix[i][j] = matrix[n-1-j][i]; 12 matrix[n-1-j][i] = matrix[n-1-i][n-1-j]; 13 matrix[n-1-i][n-1-j] = matrix[j][n-1-i]; 14 matrix[j][n-1-i] = temp; 15 } 16 } 17 return; 18 } 19 };