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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//原地旋转90度。 //1.沿主对角线所有元素交换 //2.沿着垂直中轴线方向所有元素交换 class Solution { public: void rotate(vector<vector<int>>& matrix) { for(int i = 0; i < matrix.size(); i++) for(int j = 0; j <= i; j++) swap(matrix[i][j], matrix[j][i]); for(int i = 0, j = matrix.size() - 1; i < j; i++, j--) for(int k = 0; k < matrix.size(); k++) swap(matrix[k][i], matrix[k][j]); } }; |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼