[LeetCode] Rotate Image
2013-10-29 22:22
417 查看
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?
问题描述:给定一个n*n的二维矩阵,它代表的是一幅图片,将这个图片,也就是矩阵顺时针旋转90度。
我采用的方法和之前Spiral Matrix II类似,一圈一圈地向里面推进。然后用一个变量来作为中转。
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
问题描述:给定一个n*n的二维矩阵,它代表的是一幅图片,将这个图片,也就是矩阵顺时针旋转90度。
我采用的方法和之前Spiral Matrix II类似,一圈一圈地向里面推进。然后用一个变量来作为中转。
class Solution { public: void rotate(vector<vector<int> > &matrix) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int n = matrix.size(); int temp = 0; int i = 0, j = 0; int start = 0, end = n-1; int index = 0; int width = 0; width = n-1; while(start < end) { for(j = start; j < end; j++) { temp = matrix[index][j]; matrix[index][j] = matrix[width-j][index]; matrix[width-j][index] = matrix[width-index][width-j]; matrix[width-index][width-j] = matrix[j][width-index]; matrix[j][width-index] = temp; } index++; start++; end--; } } };
相关文章推荐
- Struts2——(8)struts2中文件的上传
- Struts2——(8)struts2中文件的上传
- 【LeetCode】Linked List Cycle
- JBoss提供的常用的对称加密算法
- [置顶] (奇迹冬瓜)坦克大战[MFC框架]
- [World Wind学习]22.相机高度和瓦片等级计算
- C语言
- apple风格的html文档生成
- ffmpeg 学习 之几个基本数据结构之间联系
- (递归)汉诺塔
- iOS开发-小知识(二)
- HDU3468:A Simple Problem with Integers(线段树区间更新+lazy)
- IOS7 新特性(针对同样讨厌更新后IOS7的开发者)
- [置顶] bzoj 1036 树的统计Count 点权值模板
- Python Cookbook学习记录 ch3_3/4_2013/10/29
- Sublime Text 配置使用总结(不断更新....)
- SPOJ 8222. Substrings(后缀自动机模板)
- All in All 找子串 水题
- 三国演义 ----感悟
- fcntl的区域锁定