首页 > 其他 > 详细

a common method to rotate the image

时间:2015-08-27 22:46:03      阅读:240      评论:0      收藏:0      [点我收藏+]
 1 /*
 2  * clockwise rotate
 3  * first reverse up to down, then swap the symmetry 
 4  * 1 2 3     7 8 9     7 4 1
 5  * 4 5 6  => 4 5 6  => 8 5 2
 6  * 7 8 9     1 2 3     9 6 3
 7 */
 8 void rotate(vector<vector<int> > &matrix) {
 9     reverse(matrix.begin(), matrix.end());
10     for (int i = 0; i < matrix.size(); ++i) {
11         for (int j = i + 1; j < matrix[i].size(); ++j)
12             swap(matrix[i][j], matrix[j][i]);
13     }
14 }
15 
16 /*
17  * anticlockwise rotate
18  * first reverse left to right, then swap the symmetry
19  * 1 2 3     3 2 1     3 6 9
20  * 4 5 6  => 6 5 4  => 2 5 8
21  * 7 8 9     9 8 7     1 4 7
22 */
23 void anti_rotate(vector<vector<int> > &matrix) {
24     for (auto vi : matrix) reverse(vi.begin(), vi.end());
25     for (int i = 0; i < matrix.size(); ++i) {
26         for (int j = i + 1; j < matrix[i].size(); ++j)
27             swap(matrix[i][j], matrix[j][i]);
28     }
29 }

这边有一个题目链接可以练习。

 

a common method to rotate the image

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

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