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

leetcode 48. Rotate Image

时间:2015-03-18 20:09:22      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

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?

[Solution]

 1 void rotate(vector<vector<int> > &matrix) 
 2     {
 3         int i, j;
 4         
 5         for (i = 0; i < matrix.size(); i++)         // swap by y=x
 6             for (j = i + 1; j < matrix.size(); j++)
 7                 swap(matrix[i][j], matrix[j][i]);
 8                 
 9         for (i = 0; i < matrix.size(); i++)  // swap by y=n/2
10             for (j = 0; j < matrix.size() / 2; j++)
11                 swap(matrix[i][j], matrix[i][matrix.size() - j - 1]);
12     }

 

leetcode 48. Rotate Image

标签:

原文地址:http://www.cnblogs.com/ym65536/p/4347889.html

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