码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode --- Rotate Image

时间:2014-06-09 15:28:14      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

题目链接

题意: 给出 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

标签:c   style   class   blog   code   java   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!