LeetCode-----48. Rotate Image(二维矩阵旋转90度)
2016-10-24 19:39
871 查看
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?
坐标旋转(顺时针):
0°/360°:arr[i][j]
90°(相当于逆时针270°):a[i][j]=b[j][n-i-1];
180°:a[i][j] == b[n - i - 1][n - j -1]
270°:a[i][j] == b[n - j - 1][i]
//转置矩阵 行变列
创建一个新的矩阵:b[i][j] = a[j][i];
思路一:
对原矩阵进行求转置矩阵,转置矩阵每一行进行反转即可得到旋转90°后的矩阵
思路二:cmp
判断AB矩阵旋转角度:
import java.util.Scanner;
public class SwitchMartix{
private static Scanner sc=new Scanner(System.in);
private static int n; //方阵规模 //产生n阶方阵 public static int[][] getMatrix(){
int[][]A=new int
;
for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ A[i][j]=sc.nextInt(); } } return A; } //获取旋转角度 angle: -1表示不是旋转矩阵;旋转矩阵角度——0 90 180 270
public static int getAngle(int[][]a,int[][]b){ int angle = 0;
for(int i = 0; i < n; i ++){
for(int j = 0; j < n; j ++){
//开始遍历
if(angle == 0){ //假设修正法 角度依次增加 if(a[i][j] == b[i][j]){
continue; }else{
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
坐标旋转(顺时针):
0°/360°:arr[i][j]
90°(相当于逆时针270°):a[i][j]=b[j][n-i-1];
180°:a[i][j] == b[n - i - 1][n - j -1]
270°:a[i][j] == b[n - j - 1][i]
//转置矩阵 行变列
创建一个新的矩阵:b[i][j] = a[j][i];
思路一:
对原矩阵进行求转置矩阵,转置矩阵每一行进行反转即可得到旋转90°后的矩阵
思路二:cmp
public class Solution { public void rotate(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return; } int length = matrix.length; for (int i = 0; i < length / 2; i++) { for (int j = 0; j < (length + 1) / 2; j++){ int tmp = matrix[i][j]; matrix[i][j] = matrix[length - j - 1][i]; matrix[length -j - 1][i] = matrix[length - i - 1][length - j - 1]; matrix[length - i - 1][length - j - 1] = matrix[j][length - i - 1]; matrix[j][length - i - 1] = tmp; } } } }
判断AB矩阵旋转角度:
import java.util.Scanner;
public class SwitchMartix{
private static Scanner sc=new Scanner(System.in);
private static int n; //方阵规模 //产生n阶方阵 public static int[][] getMatrix(){
int[][]A=new int
;
for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ A[i][j]=sc.nextInt(); } } return A; } //获取旋转角度 angle: -1表示不是旋转矩阵;旋转矩阵角度——0 90 180 270
public static int getAngle(int[][]a,int[][]b){ int angle = 0;
for(int i = 0; i < n; i ++){
for(int j = 0; j < n; j ++){
//开始遍历
if(angle == 0){ //假设修正法 角度依次增加 if(a[i][j] == b[i][j]){
continue; }else{
angle = 90; } }
if(angle == 90){ //假设修正法
if(a[i][j] == b[j][n - i - 1]){ continue; }else{
angle = 180; } }
if(angle == 180){ //假设修正法 if(a[i][j] == b[n - i - 1][n - j -1]){ continue; }else{ angle = 270; } } if(angle == 270){ //假设修正法 if(a[i][j] == b[n - j - 1][i]){ continue; }else{
return -1; //不是旋转矩阵,直接返回 } } }//for }//for return angle; }
相关文章推荐
- [leetcode 48] Rotate Image--------矩阵旋转90度
- LeetCode 48. Rotate Image 二维矩阵90度翻转问题,坐标推算,空间利用
- LeetCode-48-Rotate Image 矩阵旋转90
- leetcode 48. Rotate Image 矩阵旋转
- 矩阵旋转系列之【LeetCode】Rotate Image && 【九度】题目1164:旋转矩阵
- [LeetCode]—Rotate Image 矩阵90度翻转
- LeetCode Rotate Image(矩阵的旋转)
- Leetcode刷题记——48. Rotate Image(旋转图像)
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
- LeetCode 48. Rotate Image(旋转图片)
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
- LeetCode 48. Rotate Image 数组90度翻转(要求空间复杂度)
- [LeetCode] 48. Rotate Image 旋转图像
- 方形矩阵顺时针旋转90度 Rotate Image
- leetcode解题之48. Rotate Image java版(旋转图像)
- LeetCode-Rotate Image-图像旋转-矩阵元素操作
- [LeetCode] Rotate Image n-by-n矩阵顺时针旋转
- Leetcode:48. Rotate Image(数组选择90度)
- LeetCode 48. Rotate Image(旋转图像)