Leetcode48 Rotate Image
来源:互联网 发布:电脑打电话软件下载 编辑:程序博客网 时间:2024/05/24 23:12
Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Solution1
- 这道题无非是一些边界条件的把握。
public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i=0;i<n/2;i++){ for(int j=i;j<n-i-1;j++){ int temp = matrix[i][j]; matrix[i][j] = matrix[n-j-1][i]; matrix[n-j-1][i] = matrix[n-i-1][n-j-1]; matrix[n-i-1][n-j-1] = matrix[j][n-i-1]; matrix[j][n-i-1] = temp; } } }}
Solution2
- 有更加trick的解法,如下
public class Solution { public void rotate(int[][] matrix) { for(int i=0,j=matrix.length-1;i<j;i++,j--){ int[] temp = matrix[i]; matrix[i] = matrix[j]; matrix[j] = temp; } for(int i=0;i<matrix.length;i++){ for(int j=0;j<i;j++){ int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } }}
更详细的解释在顺时针和逆时针都可以用类似方法
0 0
- Leetcode48 Rotate Image
- leetcode48~Rotate Image
- LeetCode48. Rotate Image
- LeetCode48. Rotate Image题解
- leetcode48. Rotate Image
- Leetcode48. Rotate Image
- Leetcode48 Rotate Image
- leetcode48. Rotate Image
- LeetCode48——Rotate Image
- LeetCode48/189 Rotate Image/Rotate Array
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- 【CF 459D】 Pashmak and Parmida's problem
- Xcode 7.0 beta 5 相对于 beta 4改变
- 台风“苏迪罗”夜间登陆福建最大风力15级
- 转:“智齿”到底该不该拔?
- 关于synchronized具备原子性的问题
- Leetcode48 Rotate Image
- HDU 2191
- HDU2102 A计划 (基础BFS)
- JAVA中CLASS.FORNAME的含义
- flowplayer视频播放插件
- 27Remove Element
- C++ linux epoll并发服务器模型初探
- 代码从记事本拷贝到 source insight 工程出现中文乱码解决办法
- 我在使用的Xcode插件