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

Leetcode Rotate Image

时间:2014-07-07 15:13:49      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   io   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?

矩阵的旋转

1 2 3

4 5 6

7 8 9

顺时针旋转90度为

7 4 1

8 5 2

9 6 3

(i,j)旋转后变成 (j,n-i-1)

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        int n = matrix.size();
        for(int i = 0 ;i  < n/2; ++ i){
            for(int j = i; j < n - i-1; ++ j){
                swap(matrix[i][j],matrix[j][n-i-1]); 
                swap(matrix[i][j],matrix[n-i-1][n-j-1]);
                swap(matrix[i][j],matrix[n-j-1][i]);
            }
        }
    }
};

 

Leetcode Rotate Image,布布扣,bubuko.com

Leetcode Rotate Image

标签:style   blog   color   for   io   leetcode   

原文地址:http://www.cnblogs.com/xiongqiangcs/p/3815243.html

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