48. Rotate Image
来源:互联网 发布:爱淘宝的红包怎么用 编辑:程序博客网 时间:2024/06/05 22:49
48. Rotate Image
题目
给你一个n x n 的二维矩阵代表一个图片。
将这个图片旋转90°(顺时针)。
注意:
你必须在原位置的旋转这个图片,意味着你必须直接修改输入的二维矩阵,不要重新分配另外一个二维矩阵来进行旋转。
例子1:
输入矩阵 = [ [1,2,3], [4,5,6], [7,8,9]],在原空间旋转输入矩阵后变成:[ [7,4,1], [8,5,2], [9,6,3]]
例子2:
输入矩阵 =[ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16]], 旋转后变成:[ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11]]
代码块
class Solution { public void rotate(int[][] matrix) { int len = matrix.length; for(int i = 0; i < len / 2; i ++ ){ for( int j = 0; j < (len + 1)/2; j++ ){ int temp = matrix[i][j]; matrix[i][j] = matrix[len - 1 - j][i]; matrix[len - 1 - j][i] = matrix[len - 1 - i][len - 1 - j]; matrix[len - 1 - i][len - 1 - j] = matrix[j][len - 1 - i]; matrix[j][len - 1 - i] = temp; } } } public static void main(String[] arg){ int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}}; rotate(matrix); for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ System.out.print(matrix[i][j]+" "); } System.out.println(); } }}
代码分析
旋转数组:
方法一:先转置,再flip(翻转),如图所示,但是有两层循环,复杂度不是最优的。
方法二:使用模拟法(可参考五大基础算法)。
把他们旋转的规律表示出来。
如下图所示:
编程时可以仿照交换方法。
阅读全文
0 0
- [LeetCode]48.Rotate Image
- LeetCode 48. Rotate Image
- LeetCode 48.Rotate Image
- LeetCode --- 48. Rotate Image
- [Leetcode] 48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- [leetcode] 48. Rotate Image
- Leetcode 48. Rotate Image
- 48. Rotate Image LeetCode
- [LeetCode]48. Rotate Image
- 48. Rotate Image
- 48. Rotate Image
- leetcode 48. Rotate Image
- Leetcode 48. Rotate Image
- LeetCode 48. Rotate Image
- 机器学习实战第五章Logistic回归的运行的小错误
- 11.15课堂笔记、作业
- 三元运算符和this
- px4源码学习四--固定翼姿态控制模块阅读
- KVM详解,太详细太深入了,经典
- 48. Rotate Image
- arcgis soe调试配置eclipse失败
- 安装Ubuntu后要做的事
- Safari低版本不兼容滚动条解决方案
- 记录一下,关于qq空间分享的Icon不显示的问题
- Mac 配置java环境变量
- 创建虚拟目录http://localhost:850/失败,错误:无法创建网....
- C语言程序设计 PAT题目
- Python编程从入门到实践:习题3-8~3-10