Leetcode: 48.Rotate Image(Week4, Medium)
来源:互联网 发布:特大电信网络诈骗案 编辑:程序博客网 时间:2024/05/14 19:56
本题采用了两种解法:常规解法与特殊解法
Leetcode 48
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.Example 1:
Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
>Example 2:Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
题意:实现矩阵的顺时针旋转
思路:
I. 常规思路:总结规律。
- (0,0)–>(0,3)
- (0,1)–>(1,3)
- (0,2)–>(2,3)
- (0,3)–>(3,3)
- (1,0)–>(0,2)
- (1,1)–>(1,2)
- (1,2)–>(2,2)
(1,3)–>(3,2)
… (以此类推)
由以上我们能得到规律:
(i, j) = (j, size()-i-1)
所以代码如下:
/*2017/10/1 Rotate_Image思路:本题要求我们提供一个算法,将矩阵进行旋转。 本题是顺时针旋转。可以通过找规律,来进行旋转! */class Solution {public: void rotate(vector<vector<int>>& matrix) { vector<bool> isSwap(matrix.size()*matrix[0].size(), 0); for (int i = 0; i < matrix.size(); i++) { for (int j = 0; j < matrix[i].size(); j++) { if (!isSwap[i*matrix[i].size()+j]) { int temp = matrix[j][matrix[i].size()-i-1]; matrix[j][matrix[i].size()-i-1] = matrix[i][j]; matrix[i][j] = temp; isSwap[i*matrix[i].size()+j] = 1; isSwap[j*matrix[i].size()+matrix[i].size()-i-1] = 1; } } } }};
II. 特殊解法:先将行倒置,然后再对矩阵进行转置。
/* clockwise rotate first reverse up to down, then swap the symmetry 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 */ void rotate(vector<vector<int> > &matrix) { reverse(matrix.begin(), matrix.end()); for (int i = 0; i < matrix.size(); ++i) { for (int j = i + 1; j < matrix[i].size(); ++j) swap(matrix[i][j], matrix[j][i]); } }
III. 如果是逆时针呢,可以采用:先将各行的元素进行倒置,然后对矩阵转置
/* anticlockwise rotate first reverse left to right, then swap the symmetry 1 2 3 3 2 1 3 6 9 4 5 6 => 6 5 4 => 2 5 8 7 8 9 9 8 7 1 4 7*/void anti_rotate(vector<vector<int> > &matrix) { for (auto vi : matrix) reverse(vi.begin(), vi.end()); for (int i = 0; i < matrix.size(); ++i) { for (int j = i + 1; j < matrix[i].size(); ++j) swap(matrix[i][j], matrix[j][i]); }}
以上内容皆为本人观点,欢迎大家匹配和指导,我们一起探讨!
- Leetcode: 48.Rotate Image(Week4, Medium)
- LeetCode 48. Rotate Image (Medium)
- Leetcode 48. Rotate Image (Medium) (cpp)
- Leetcode 48. Rotate Image (Medium) (java)
- [Leetcode 48, medium] Rotate Image
- LeetCode-48-Rotate Image(模拟)-Medium
- 【Leet Code】48. Rotate Image---Medium
- [LeetCode]48.Rotate Image
- LeetCode 48. Rotate Image
- LeetCode 48.Rotate Image
- LeetCode --- 48. Rotate Image
- [Leetcode] 48. Rotate Image
- [leetcode] 48. Rotate Image
- Leetcode 48. Rotate Image
- 48. Rotate Image LeetCode
- [LeetCode]48. Rotate Image
- leetcode 48. Rotate Image
- Leetcode 48. Rotate Image
- [set] 鸟语字典
- GYM 101243 J. Architect of Your Own Fortune【二分匹配】
- Linux常用命令
- 欢迎使用CSDN-markdown编辑器
- Django--Cookie和Session
- Leetcode: 48.Rotate Image(Week4, Medium)
- K:注解简介
- 斜率dp优化两题(poj 3709 hdu 3507)
- okhttp使用
- EMD经验模态分解——分析时间序列
- 给定有向图,每条边只能用一次,求给定两点间有几条最短路。
- apache文件上传组件
- K:注解语法简介
- 深入理解JVM(一)-JVM内存模型