本文主要是介绍【LeetCode】48 Rotate Image 【九度】题目1164:旋转矩阵,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Rotate ImageTotal Accepted: 4289 Total Submissions: 14183 My Submissions
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?
旋转矩阵,这个比九度 题目1164:旋转矩阵要简单一些。
只是顺时针旋转90度。
假设原矩阵名称为arrayA,旋转后矩阵为arrayB。
举例4*4矩阵并给出旋转示意图,以及关系。
原图:
关系arrayB[i][j] == arrayA[N-j-1][i]
关系arrayB[i][j] == arrayA[N-i-1][N-j-1]
关系arrayB[i][j] == arrayA[j][N-i-1]
LeetCode Java AC
public class Solution {public void rotate(int[][] matrix) {if(matrix == null){return;}int n = matrix.length;int array[][] = new int[n][n];for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){array[i][j] = matrix[n-1-j][i];}}for(int i = 0; i < n; i++){for(int j = 0; j < n; j++){matrix[i][j] = array[i][j];}}}
}
九度
题目1164:旋转矩阵 Java AC
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while (scanner.hasNext()) {int N = scanner.nextInt();int arrayA[][] = new int[N][N];for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {int num = scanner.nextInt();arrayA[i][j] = num;}}int arrayB[][] = new int[N][N];for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {int num = scanner.nextInt();arrayB[i][j] = num;}}int firstNum = revolve0(arrayA,arrayB,N);int secNum =revolve90(arrayA,arrayB,N);int thiNum = revolve180(arrayA,arrayB,N);int fourNum = revolve270(arrayA,arrayB,N);if (firstNum == -1 && secNum == -1 && thiNum == -1 && fourNum == -1) {System.out.println(-1);}else {List<Integer> arrayList = new ArrayList<Integer>();if (firstNum != -1) {arrayList.add(firstNum);}if (secNum != -1) {arrayList.add(secNum);}if (thiNum != -1) {arrayList.add(thiNum);}if (fourNum != -1) {arrayList.add(fourNum);}Collections.sort(arrayList);System.out.println(arrayList.get(0));}}}private static int revolve0(int[][] arrayA, int[][] arrayB, int N) {for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {if (arrayB[i][j] == arrayA[i][j]) {continue;}else {return -1;}}}return 0;}private static int revolve90(int[][] arrayA, int[][] arrayB, int N) {for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {if (arrayB[i][j] == arrayA[N-j-1][i]) {continue;}else {return -1;}}}return 90;}private static int revolve180(int[][] arrayA, int[][] arrayB, int N) {for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {if (arrayB[i][j] == arrayA[N-i-1][N-j-1]) {continue;}else {return -1;}}}return 180;}private static int revolve270(int[][] arrayA, int[][] arrayB, int N) {for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {if (arrayB[i][j] == arrayA[j][N-i-1]) {continue;}else {return -1;}}}return 270;}
}
/**************************************************************Problem: 1164User: wzqwsrfLanguage: JavaResult: AcceptedTime:110 msMemory:18660 kb
****************************************************************/
这篇关于【LeetCode】48 Rotate Image 【九度】题目1164:旋转矩阵的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!