首页 > 其他 > 详细

LeetCode --- Rotate Image

时间:2014-06-09 15:27:58      阅读:370      评论:0      收藏:0      [点我收藏+]

题目链接

题意: 给出 n * n的矩阵,要求将矩阵顺时针旋转90°(不使用额外空间)

bubuko.com,布布扣
 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 };
bubuko.com,布布扣

 

LeetCode --- Rotate Image,布布扣,bubuko.com

LeetCode --- Rotate Image

原文:http://www.cnblogs.com/Stomach-ache/p/3776458.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!