-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathProblem48_rotateImage.java
More file actions
141 lines (127 loc) · 3.8 KB
/
Problem48_rotateImage.java
File metadata and controls
141 lines (127 loc) · 3.8 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.longluo.top100;
import com.longluo.datastructure.ArrayUtils;
/**
* 48. 旋转图像
* <p>
* 给定一个 n × n 的二维矩阵表示一个图像。
* <p>
* 将图像顺时针旋转 90 度。
* <p>
* 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
* <p>
* 示例 1:
* 给定 matrix =
* [
* [1,2,3],
* [4,5,6],
* [7,8,9]
* ],
* 原地旋转输入矩阵,使其变为:
* [
* [7,4,1],
* [8,5,2],
* [9,6,3]
* ]
* <p>
* 示例 2:
* 给定 matrix =
* [
* [ 5, 1, 9,11],
* [ 2, 4, 8,10],
* [13, 3, 6, 7],
* [15,14,12,16]
* ],
* 原地旋转输入矩阵,使其变为:
* [
* [15,13, 2, 5],
* [14, 3, 4, 1],
* [12, 6, 8, 9],
* [16, 7,10,11]
* ]
* <p>
* 示例 3:
* 输入:matrix = [[1]]
* 输出:[[1]]
* <p>
* 示例 4:
* 输入:matrix = [[1,2],[3,4]]
* 输出:[[3,1],[4,2]]
* <p>
* https://leetcode.com/problems/rotate-image/
*/
public class Problem48_rotateImage {
// BF time: O(n^2) space: O(n^2)
public static void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int n = matrix.length;
int[][] newMatrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
newMatrix[j][n - 1 - i] = matrix[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = newMatrix[i][j];
}
}
}
// Inplace Switch time: O(n^2) space: O(1)
public static void rotate_inplace(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return;
}
int n = matrix.length;
for (int i = 0; i < n / 2; ++i) {
for (int j = 0; j < (n + 1) / 2; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n - j - 1][i];
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
matrix[j][n - i - 1] = temp;
}
}
}
// Mirror time: O(n^2) space: O(1)
public static void rotate_mirrow(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n - 1 - i][j];
matrix[n - 1 - i][j] = temp;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
public static void main(String[] args) {
int[][] tst1 = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
rotate(tst1);
rotate_inplace(tst1);
rotate_mirrow(tst1);
System.out.println("[[7, 4, 1],[8, 5, 2],[9, 6, 3]] ?= " + ArrayUtils.print2DArray(tst1));
int[][] tst2 = new int[][]{{5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}};
rotate(tst2);
rotate_inplace(tst2);
rotate_mirrow(tst2);
System.out.println("[[15, 13, 2, 5],[14, 3, 4, 1],[12, 6, 8, 9],[16, 7, 10, 11]] ?= " + ArrayUtils.print2DArray(tst2));
int[][] tst3 = new int[][]{{1}};
rotate(tst3);
rotate_inplace(tst3);
rotate_mirrow(tst3);
System.out.println("[[1]] ?= " + ArrayUtils.print2DArray(tst3));
int[][] tst4 = new int[][]{{1, 2}, {3, 4}};
rotate(tst4);
rotate_inplace(tst4);
rotate_mirrow(tst4);
System.out.println("[[3, 1],[4, 2]] ?= " + ArrayUtils.print2DArray(tst4));
}
}