首页 > 代码库 > LeetCode --- Rotate Image
LeetCode --- Rotate Image
题目链接
题意: 给出 n * n的矩阵,要求将矩阵顺时针旋转90°(不使用额外空间)
1 /*
2 Basically, divide the array into 4 along the diagonals,
3 then for each element in the top quadrant, place it into
4 the slot 90 degrees cw, and the old 90 in 180 degrees cw,
5 and the old 180 in 270 degrees , and the old 270 in
6 the original place.
7 */
8 class Solution {
9 public:
10 void rotate(vector<vector<int> > &matrix) {
11 int n = (int)matrix.size();
12 for (int i = 0; i < n/2; i++) for (int j = i; j < n-1-i; j++) {
13 swap(matrix[i][j], matrix[j][n-1-i]);
14 swap(matrix[i][j], matrix[n-1-i][n-1-j]);
15 swap(matrix[i][j], matrix[n-1-j][i]);
16 }
17 }
18 };
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。