您的位置:首页 > 其它

[LeetCode] Rotate Image 解题报告

2013-01-02 07:49 441 查看
You are given an n[/i] x n[/i] 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:
Could you do this in-place?» Solve this problem

[解题思路]
如下图,首先沿逆对角线翻转一次,然后按x轴中线翻转一次。



[code]1:    void rotate(vector<vector<int> > &matrix) {
2:      // Start typing your C/C++ solution below
3:      // DO NOT write int main() function
4:      int len = matrix[0].size();
5:      for(int i =0; i<len-1; i++)
6:      {
7:        for(int j=0;j<len-i;j++)
8:        {
9:          swap(matrix[i][j], matrix[len-1-j][len-1-i]);
10:        }
11:      }
12:      for(int i =0; i<len/2; i++)
13:      {
14:        for(int j=0;j<len;j++)
15:        {
16:          swap(matrix[i][j], matrix[len-i-1][j]);
17:        }
18:      }
19:    }
20:    void swap(int& a1, int&a2)
21:    {
22:      int temp = a1;
23:      a1=a2;
24:      a2=temp;
25:    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: