leetcode 矩阵旋转
来源:互联网 发布:尔雅网络课程登录入口 编辑:程序博客网 时间:2024/05/16 01:07
昨天做了矩阵旋转,刚开始思路不太对,还真想着要转呢。
后来才意识到不能真转,实现起来比较困难,就算真的实现了效率也不会高。然后还是学习了别人的方法,这里记录一下吧。
题目:给定1个n*n的二维数组,顺时针旋转这个数组。思路是将给定数组按行交换,第一行和最后一行互换,第二行和倒数第二行互换----然后求交换后矩阵的对称矩阵即可。
1 2 3 7 8 9 7 4 1
4 5 6 => 4 5 6 => 8 5 2
7 8 9 1 2 3 9 6 3
对于3*3数组就是上面的过程,很直观很好操作。
代码:
void rotate(vector<vector<int> > &matrix){ reverse(matrix.begin(),matrix.end()); int size=matrix.size(); for(int i=0;i<size;++i) { for(int j=i+1;j<size;++j) { swap(matrix[i][j],matrix[j][i]); } }}
这个方法同样可以用于矩阵的逆时针旋转,只不过把第一步的交换行改变为交换列(交换列的实现要稍麻烦一点)。
代码:
void rotate(vector<vector<int> > &matrix){ auto row=matrix.begin(); for(;row!=matrix.end();++row) { reverse(row.begin(),row.end()); } int size=matrix.size(); for(int i=0;i<size;++i) { for(int j=i+1;j<size;++j) { swap(matrix[i][j],matrix[j][i]); } }}
0 0
- leetcode 矩阵旋转
- LeetCode-矩阵旋转
- Rotate Image 旋转矩阵 @LeetCode
- leetcode之图片(矩阵)旋转
- Spiral Matrix 输出旋转矩阵@LeetCode
- [LeetCode] Spiral Matrix 旋转打印矩阵
- LeetCode Rotate Image(矩阵的旋转)
- leetcode-48 Rotate Image 旋转矩阵
- leetcode 48. Rotate Image-矩阵旋转
- leetcode 48. Rotate Image(矩阵旋转)
- leetcode 48. Rotate Image 矩阵旋转
- LeetCode-48-Rotate Image 矩阵旋转90
- Leetcode: 在原矩阵上实现矩阵的旋转(Python)
- 旋转矩阵
- 矩阵旋转
- 旋转矩阵
- 旋转矩阵
- 旋转矩阵
- 解析JSON将数据存入sqlserver数据库中
- axure中继器的使用
- 回调函数教程(二):Android中ListView点击事件回调实例
- Openfire3.9.3源代码导入eclipse中开发配置指南
- 一个农夫的故事
- leetcode 矩阵旋转
- 2015-1-24
- Android 软键盘弹起隐藏的监听
- mysql存储过程
- E: dpkg 被中断,您必须手工运行 sudo dpkg --configure -a 解决此问题
- UVA 10566 && POJ 2507 Crossed Ladders (几何)
- 从陈年往事说说凡客的教训
- cortex_m3_stm32嵌入式学习笔记(十五):待机唤醒实验(WK_UP外部中断)
- 三消类游戏的核心算法