Created
July 16, 2017 07:54
-
-
Save jianminchen/8f08c671bacb4d3bc7800948ef6cf4c4 to your computer and use it in GitHub Desktop.
Rotate matrix - study code written in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public static int[][] rotate(int[][] matrix, int flag) | |
{ | |
if(matrix == null || matrix.length == 0) return matrix; | |
int m = matrix.length, n = matrix[0].length; | |
int[][] res = new int[n][m]; | |
for(int i = 0; i < m; i++) | |
{ | |
for(int j = 0; j < n; j++) res[j][i] = matrix[i][j]; | |
} | |
if(flag == 1) | |
{ | |
for(int i = 0; i < n; i++) | |
{ | |
for(int j = 0; j < m / 2; j++) | |
{ | |
int tmp = res[i][j]; | |
res[i][j] = res[i][m - 1 - j]; | |
res[i][m - 1 - j] = tmp; | |
} | |
} | |
} | |
else | |
{ | |
for(int j = 0; j < m ;j++) | |
{ | |
for(int i = 0; i < n / 2; i++) | |
{ | |
int tmp = res[i][j]; | |
res[i][j] = res[n - 1 - i][j]; | |
res[n - 1 - i][j] = tmp; | |
} | |
} | |
} | |
return res; | |
} | |
public static void printMatrix(int[][] matrix) | |
{ | |
int m = matrix.length, n = matrix[0].length; | |
for(int i = 0; i < m; i++) | |
{ | |
for(int j = 0; j < n; j++) System.out.print(matrix[i][j] + " "); | |
System.out.println(""); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
int[][] input = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; | |
int[][] output1 = rotate(input, 0); | |
int[][] output2 = rotate(input, 1); | |
System.out.println("input"); | |
printMatrix(input); | |
System.out.println("output1"); | |
printMatrix(output1); | |
System.out.println("output2"); | |
printMatrix(output2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment