Rotate Image
来源:互联网 发布:java知识分享网 编辑:程序博客网 时间:2024/06/06 09:05
Rotate Image
我的思路:
1、顺时针将n * n的矩阵在旋转90度,空间复杂度O(1)。
2、双重循环,单个循环内将每条边的数据赋值,使用一个临时变量保存最后一个要赋值的数据值。
3、写好每个控制变量,6ms过,讨论区还有一些其他方法,都差不多。考虑好矩阵旋转的特性。
代码如下:
void rotate(vector<vector<int> > &matrix) { int temp; int m = matrix[0].size(); int n = m; for (int i = 0; i < n / 2; i++) { for (int j = i; j < m - 1 - i; j++) { temp= matrix[i][j]; matrix[i][j]= matrix[n - 1 - j][i]; matrix[n - 1 - j][i]= matrix[n - 1 - i][m - 1 -j]; matrix[n - 1 - i][m - 1 - j]= matrix[j][m - 1 - i]; matrix[j][m - 1 - i]= temp; } } }
别人思路:
1、将数组数据按照对角线交换之后reverse数组,就能得到旋转的数据。
0 0
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- 我心中真正的程序员
- 前端进阶学习(四) --css框架再探
- 大三上------期末总结
- AndroidXML文件属性的详解
- PHP 连接 SQL2008
- Rotate Image
- boost库学习之 lexical_cast
- POJ 2337 Catenyms(有向图欧拉路径判定&&打印)
- OpenCV学习之CvMat的用法详解及实例(一)
- Java EE
- 2015-1-30-工作日志-刘婉
- Background-Output
- NYOJ 组合数 32(DFS)
- POJ 题目2253 Frogger(最短路变形)