Rotate Image - LeetCode
来源:互联网 发布:cad看图软件mac版免费 编辑:程序博客网 时间:2024/06/09 14:00
题目有两种思路:
1.先将矩阵沿主对角线旋转,再左右翻折即可。
2.直接将每个元素交换到对应的位置上去。
思路一耗时较长,但我们可以利用思路1来计算每个元素交换后的位置。
假设原位置为(i,j),沿主对角线翻折后位置为(j,i),再左右翻折后的位置就应该为(j,n - i - 1)。那么对于翻后位置为(i,j)的点,翻转前的位置应该是(n - 1 - j,i),对于由外而内的每一层都做对换,即可得到答案。
class Solution {public: void rotate(vector<vector<int>>& matrix) { int size = matrix.size(); int starti = 0, startj = 0; int lenth = size; while (lenth >= 2) { for (int t = 0 ; t < lenth - 1 ; ++t) { int i = starti, j = startj + t; int k = 3; while(k--) { int ii = size - 1 - j; int jj = i; swap(matrix[ii][jj],matrix[i][j]); i = ii; j = jj; } } starti++; startj++; lenth -= 2; } }};
这里主要是给出旋转后点的计算方法,具体的程序实现还有很多,比如下面给出的算法,不是利用我这里每次都计算新的位置,而是直接计算将新位置放在程序中:
http://blog.csdn.net/kenden23/article/details/17200067
0 0
- LeetCode: Rotate Image
- [Leetcode] Rotate Image
- LeetCode : Rotate Image
- [LeetCode] Rotate Image
- leetcode 67: Rotate Image
- [leetcode] Rotate Image
- [Leetcode] Rotate Image
- [LeetCode]Rotate Image
- LeetCode-Rotate Image
- [leetcode] Rotate Image
- LeetCode - Rotate Image
- leetcode之Rotate Image
- LeetCode - Rotate Image
- Leetcode: Rotate Image
- LeetCode:Rotate Image
- Leetcode: Rotate Image
- Leetcode Rotate Image
- leetcode Rotate Image
- 转自:系统运维 » 导出Windows服务器下的Oracle数据库并导入到Linux服务器下的Oracle数据库中
- iOS开发中coredata的使用和数据持有化
- dojo使用Programmatic(程序)方式创建form组件没有lable属性的解决
- 第六周 建立链栈算法库
- *第六周*数据结构实践项目二【建设链栈算法库】
- Rotate Image - LeetCode
- 让智能硬件,真正改变生活
- 怎么使ofbiz连接Mysql
- android 的动画类型
- 闲暇时间开发的个人app
- 高新技术和供应链结合,成智能硬件发展“新爆点”
- iOS中 数据持久化方式
- 去IOE
- 第一个例子:查询链接到节点的动画曲线