个人记录-LeetCode 48. Rotate Image
来源:互联网 发布:npm 淘宝镜像下载失败 编辑:程序博客网 时间:2024/05/05 21:58
问题:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
代码示例:
这个问题其实上就是做一个坐标变换。
1、使用额外空间
public class Solution { public void rotate(int[][] matrix) { if (matrix == null || matrix.length < 1 || matrix[0].length < 1) { return; } if (matrix.length != matrix[0].length) { return; } //自己画个图就可以得出结论,旋转90度后,第i行将被放到len-1-i列,第j列将变到第j行 //使用额外空间,不用保存记录,比较简单 int len = matrix.length; int[][] temp = new int[len][len]; for (int i = len - 1; i >=0; --i) { for (int j = 0; j <= len - 1; ++j) { temp[j][len-1-i] = matrix[i][j]; } } for (int i = 0; i <= len-1; ++i) { System.arraycopy(temp[i], 0, matrix[i], 0, len); } }}
2、更精细的变换
变换的原理与1一样,只是写出每个数具体的变换步骤,同时优化了起止的位置。
public class Solution { public void rotate(int[][] matrix) { if (matrix == null || matrix.length < 1 || matrix[0].length < 1) { return; } if (matrix.length != matrix[0].length) { return; } int len = matrix.length; int temp; for (int i = 0; i <= (len - 1)/2; ++i) { for (int j = i; j < len - 1 - i; ++j) { 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; } } }}
0 0
- 个人记录-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
- Leetcode 48. Rotate Image
- 48. Rotate Image LeetCode
- [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
- LeetCode-48.Rotate Image
- [LeetCode] 48. Rotate Image
- 基于Redis的资源锁
- 关于unity莫名闪退、黑屏
- java异常处理机制
- 计算机图形学总结(2D)
- git 放弃本地修改 强制更新
- 个人记录-LeetCode 48. Rotate Image
- requestLayout in layout问题
- PowerDesigner使用教程
- 2-HTML中的表格及表单控件
- Scrapy安装
- jsp/servlet文件上传
- Caffe代码梳理笔记
- java 基础
- ORACLEのDUMPを作成