Leetcode刷题记——48. Rotate Image(旋转图像)
来源:互联网 发布:淘宝添加到桌面没有了 编辑:程序博客网 时间:2024/05/01 09:30
一、题目叙述:
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?
Subscribe to see which companies asked this question.
给一个n*n的数组矩阵代表一个2D图像,顺时针旋转90度,得到的图像
二、解题思路:
Medium题,简单举个例子即可得出规律。
如:
[2,5 [8, 2
8,,4] 旋转后为 4,5] 即,旋转后的第一行为旋转前第一列的逆序;第二行为第二列的逆序。。。以此类推。
(1)创建中间二维数组res,按上述规律为res赋值。
(2)将res内容复制回原数组即可。
三、源码:
import java.util.ArrayList; import java.util.Arrays;import java.util.List; public class Solution { public void rotate(int[][] matrix) {int n = matrix.length;if (n == 0) return;int [][] res = new int[n][n];for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)res[i][j] = matrix[n-j-1][i];for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)matrix[i][j] = res[i][j];System.out.print(Arrays.deepToString(matrix));return; } public static void main(String args[]) { //String a = ""; //String b = ""; // int[] digits = {0}; Solution solution = new Solution(); int[][] abc = {{2}}; // int[] b = {2,3,4}; // for(int i = 0; i < abc.length; i ++) solution.rotate(abc); // System.out.print(solution.rotate(abc)); } }
0 0
- Leetcode刷题记——48. Rotate Image(旋转图像)
- LeetCode | Rotate Image(旋转图像)
- LeetCode 48 Rotate Image(旋转图像)
- LeetCode Rotate Image旋转图像
- [LeetCode] Rotate Image 旋转图像
- LeetCode Rotate Image旋转图像
- LeetCode 48. Rotate Image(旋转)
- leetcode 48. Rotate Image(矩阵旋转)
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
- Leetcode #48. Rotate Image 图像旋转 解题报告
- LeetCode OJ 之 Rotate Image (旋转图像)
- leetcode之图像旋转(Rotate Image)
- LeetCode--Rotate Image(旋转图像)Python
- Rotate Image 旋转图像
- leetcode解题之48. Rotate Image java版(旋转图像)
- leetcode 48. Rotate Image-矩阵旋转
- leetcode 48. Rotate Image 矩阵旋转
- [LintCode] 旋转图像 Rotate Image
- ubuntu深度学习框架Matconvnet与caffe搭建 (Ubuntu14.04+Cuda7.5+Cudnn5+Matlab2015b
- 【BZOJ 2756】[SCOI2012]奇怪的游戏 网络流+二分
- mysql建立索引
- 内存被错误改变,越界写的分析
- VoIP技术(3)-语音编码算法
- Leetcode刷题记——48. Rotate Image(旋转图像)
- douglas peucker递归分治
- 【问题解决】Eclipse安装Aptana插件-(注意对应版本问题)
- 在2017年,如何将你的小米4刷上Windows 10 mobile?(后附大量图赏)
- Android动画
- Ubuntu下进行JDK的配置
- BIOS密码破解
- 实现自定义SeekBar
- Knight Shortest Path