-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathq048_RotateImage.java
More file actions
51 lines (39 loc) · 1.21 KB
/
q048_RotateImage.java
File metadata and controls
51 lines (39 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package leetcode_algorithm;
import java.util.Arrays;
/**
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?
*/
public class q048_RotateImage {
public static void main(String[] args) {
int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
new q048_RotateImage().rotate(matrix);
for (int[] nums : matrix) {
System.out.println(Arrays.toString(nums));
}
}
/**
* ½â·¨1 (ÍÆ¼ö½â·¨)
* @param matrix
*/
public void rotate(int[][] matrix) {
for(int i = 0; i< matrix.length; i++) {
for(int j = i + 1; j < matrix.length; j++) {
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i = 0; i< matrix.length; i++) {
for(int j = 0; j < matrix.length / 2; j++) {
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix.length - 1 - j];
matrix[i][matrix.length - 1 - j] = temp;
}
}
}
}