Rotate Image [LeetCode]
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?
Solution:
1 void rotate(vector<vector<int> > &matrix) { 2 int n = matrix.size(); 3 for(int i = 0; i < n / 2; i ++) { 4 for(int j = i; j < n - 1 - i; j ++){ 5 int tmp = matrix[i][j]; 6 matrix[i][j] = matrix[n - 1 - j][i]; 7 matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j]; 8 matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i]; 9 matrix[j][n - 1 - i] = tmp; 10 } 11 } 12 }