0%

algorithm-matrix

简单

有效的数独

题目描述

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
请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。

数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)


注意:

一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
空白格用 '.' 表示。


示例 1:

输入:board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:true
示例 2:

输入:board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:false
解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。


提示:

board.length == 9
board[i].length == 9
board[i][j] 是一位数字(1-9)或者 '.'

题目解答

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
public class Solution {
public boolean isValidSudoku(char[][] board) {
int[][] rows = new int[9][9];// 存放每行中每个数的个数
int[][] cols = new int[9][9];// 存放每行列中每个数的个数
int[][][] subBoard = new int[3][3][9]; // 存放每个子宫格中每个数的个数

for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char c = board[i][j];
if (c != '.') {
int value = c - '0';
int index = value - 1;
rows[i][index]++;
cols[j][index]++;
subBoard[i / 3][j / 3][index]++;

if (rows[i][index] > 1 || cols[j][index] > 1 || subBoard[i / 3][j / 3][index] > 1) {
return false;
}
}
}
}

return true;
}
}

class TestMain {

public static void main(String[] args) {
TestUtils.runTestCases(TestCase.class);
}

public static class TestCase {

public static boolean testCase1() {
char[][] board = {
{ '5', '3', '.', '.', '7', '.', '.', '.', '.' },
{ '6', '.', '.', '1', '9', '5', '.', '.', '.' },
{ '.', '9', '8', '.', '.', '.', '.', '6', '.' },
{ '8', '.', '.', '.', '6', '.', '.', '.', '3' },
{ '4', '.', '.', '8', '.', '3', '.', '.', '1' },
{ '7', '.', '.', '.', '2', '.', '.', '.', '6' },
{ '.', '6', '.', '.', '.', '.', '2', '8', '.' },
{ '.', '.', '.', '4', '1', '9', '.', '.', '5' },
{ '.', '.', '.', '.', '8', '.', '.', '7', '9' }
};

boolean result = new Solution().isValidSudoku(board);
boolean expect = true;

return TestUtils.check(result, expect);
}

public static boolean testCase2() {
char[][] board = {
{ '8', '3', '.', '.', '7', '.', '.', '.', '.' },
{ '6', '.', '.', '1', '9', '5', '.', '.', '.' },
{ '.', '9', '8', '.', '.', '.', '.', '6', '.' },
{ '8', '.', '.', '.', '6', '.', '.', '.', '3' },
{ '4', '.', '.', '8', '.', '3', '.', '.', '1' },
{ '7', '.', '.', '.', '2', '.', '.', '.', '6' },
{ '.', '6', '.', '.', '.', '.', '2', '8', '.' },
{ '.', '.', '.', '4', '1', '9', '.', '.', '5' },
{ '.', '.', '.', '.', '8', '.', '.', '7', '9' }
};

boolean result = new Solution().isValidSudoku(board);
boolean expect = false;

return TestUtils.check(result, expect);
}
}

public static class TestUtils {

public static boolean check(Object result, Object expect) {
result = normalizeObject(result);
expect = normalizeObject(expect);
boolean ok = java.util.Objects.deepEquals(result, expect);
printCheck(toString(result), toString(expect), ok);
return ok;
}

public static boolean check(Object result, String expect) {
boolean ok = java.util.Objects.equals(normalizeString(toString(result)),
normalizeString(toString(expect)));
printCheck(normalizeString(toString(result)), normalizeString(toString(expect)), ok);
return ok;
}

private static Object normalizeObject(Object o) {
if (o instanceof java.util.List) {
java.util.List<?> list = (java.util.List<?>) o;
if (list != null && !list.isEmpty() && list.get(0) instanceof java.util.List) {
o = toArrays(list);
} else {
o = toArray(list);
}
}

return o;
}

private static String toString(Object o) {
if (o instanceof Object[]) {
return java.util.Arrays.deepToString((Object[]) o);
} else if (o instanceof int[]) {
return java.util.Arrays.toString((int[]) o);
} else if (o instanceof double[]) {
return java.util.Arrays.toString((double[]) o);
} else if (o instanceof char[]) {
return java.util.Arrays.toString((char[]) o);
} else {
return java.util.Objects.toString(o);
}
}

private static String normalizeString(String s) {
boolean isArray = (s.startsWith("{") && s.endsWith("}")) || (s.startsWith("[") && s.endsWith("]"));
if (isArray) {
s = s.replace(" ", "")
.replace("'", "")
.replace("\"", "")
.replace("{", "[")
.replace("}", "]");
}

return s;
}

private static void printCheck(String result, String expect, boolean ok) {
// System.out.println();
// System.out.println("--->");

System.out.println("result: " + result);
System.out.println("expect: " + expect);

System.out.println();
if (ok) {
System.out.println("比较结果为: 正确(right)");
} else {
System.out.println("比较结果为: 错误(error)");
}

// System.out.println();
// System.out.println("<---");
}

public static void runTestCases(Class<?> clazz) {
java.lang.reflect.Method[] methods = clazz.getDeclaredMethods();
java.util.Arrays.sort(methods, (o1, o2) -> {
return o1.getName().compareTo(o2.getName());
});
for (java.lang.reflect.Method method : methods) {
if (method.getName().startsWith("test")) {
System.out.println(String.format("--- %s --->", method.getName()));

Object object = null;
try {
object = method.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println(String.format("<--- %s ---", method.getName()));

if (object instanceof Boolean) {
Boolean ok = (Boolean) object;
if (!ok) {
break;
}
} else {
throw new RuntimeException(String.format("测试用例方法%s的返回值需要为布尔类型", method.getName()));
}
}
}
}

public static Object[] toArray(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0];
}

return list.toArray();
}

public static Object[][] toArrays(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0][0];
}

Object[][] arrays = new Object[list.size()][];
for (int i = 0; i < list.size(); i++) {
arrays[i] = toArray(list.get(i));
}

return arrays;
}

@SuppressWarnings("unchecked")
public static <T> T[] genArray(Class<T> clazz, int length) {
return (T[]) java.lang.reflect.Array.newInstance(clazz, length);
}

@SuppressWarnings("unchecked")
public static <T> T[][] genArrays(Class<T> clazz, int rows, int cols) {
T[] array = genArray(clazz, 0);

T[][] arrays = (T[][]) genArray(array.getClass(), rows);
for (int i = 0; i < arrays.length; i++) {
arrays[i] = genArray(clazz, cols);
}

return arrays;
}

public static <T> T[] toArray(java.util.List<T> list, Class<T> clazz) {
if (list == null || list.isEmpty()) {
return genArray(clazz, 0);
}

T[] array = genArray(clazz, list.size());
for (int i = 0; i < array.length; i++) {
array[i] = list.get(i);
}

return array;
}

public static <T> T[][] toArrays(java.util.List<java.util.List<T>> lists, Class<T> clazz) {
if (lists == null || lists.isEmpty()) {
return genArrays(clazz, 0, 0);
}

T[][] arrays = genArrays(clazz, lists.size(), 0);
for (int i = 0; i < lists.size(); i++) {
arrays[i] = toArray(lists.get(i), clazz);
}

return arrays;
}

public static <T> java.util.List<T> toList(T[] array) {
return java.util.Arrays.asList(array);
}

public static <T> java.util.List<java.util.List<T>> toLists(T[][] arrays) {
java.util.List<java.util.List<T>> lists = new java.util.ArrayList<>();
for (T[] array : arrays) {
lists.add(toList(array));
}

return lists;
}
}
}

螺旋矩阵

题目描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。



示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]
示例 2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]


提示:

m == matrix.length
n == matrix[i].length
1 <= m, n <= 10
-100 <= matrix[i][j] <= 100

题目解答

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import java.util.ArrayList;
import java.util.List;

public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length; // 行数
int n = matrix[0].length; // 列数

int d = 0; // 移动方向 0:向右 1:向下 2:向左 3:向上
int T = 0; // 上边界
int B = m - 1; // 下边界
int L = 0; // 左边界
int R = n - 1;// 右边界

List<Integer> list = new ArrayList<>();

int i = 0;
int j = 0;
while (T <= B && L <= R) {
if (d == 0) {
// 向右
while (j <= R) {
list.add(matrix[i][j++]);
}

// 越界后归位
j--;

// 调整方向为向下并前进一步
d = 1;
i++;

// 更新上边界
T++;
} else if (d == 1) {
// 向下
while (i <= B) {
list.add(matrix[i++][j]);
}

// 越界后归位
i--;

// 调整方向为向左并前进一步
d = 2;
j--;

// 更新右边界
R--;
} else if (d == 2) {
// 向左
while (j >= L) {
list.add(matrix[i][j--]);
}

// 越界后归位
j++;

// 调整方向为向上并前进一步
d = 3;
i--;

// 更新下边界
B--;
} else if (d == 3) {
// 向上
while (i >= T) {
list.add(matrix[i--][j]);
}

// 越界后归位
i++;

// 调整方向为向右并前进一步
d = 0;
j++;

// 更新左边界
L++;
}
}

return list;
}
}

class TestMain {

public static void main(String[] args) {
TestUtils.runTestCases(TestCase.class);
}

public static class TestCase {
public static boolean testCase1() {
int[][] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};

List<Integer> result = new Solution().spiralOrder(matrix);
Integer[] expect = { 1, 2, 3, 6, 9, 8, 7, 4, 5 };

return TestUtils.check(result, expect);
}

public static boolean testCase2() {
int[][] matrix = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};

List<Integer> result = new Solution().spiralOrder(matrix);
Integer[] expect = { 1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7 };

return TestUtils.check(result, expect);
}
}

public static class TestUtils {

public static boolean check(Object result, Object expect) {
result = normalizeObject(result);
expect = normalizeObject(expect);
boolean ok = java.util.Objects.deepEquals(result, expect);
printCheck(toString(result), toString(expect), ok);
return ok;
}

public static boolean check(Object result, String expect) {
boolean ok = java.util.Objects.equals(normalizeString(toString(result)),
normalizeString(toString(expect)));
printCheck(normalizeString(toString(result)), normalizeString(toString(expect)), ok);
return ok;
}

private static Object normalizeObject(Object o) {
if (o instanceof java.util.List) {
java.util.List<?> list = (java.util.List<?>) o;
if (list != null && !list.isEmpty() && list.get(0) instanceof java.util.List) {
o = toArrays(list);
} else {
o = toArray(list);
}
}

return o;
}

private static String toString(Object o) {
if (o instanceof Object[]) {
return java.util.Arrays.deepToString((Object[]) o);
} else if (o instanceof int[]) {
return java.util.Arrays.toString((int[]) o);
} else if (o instanceof double[]) {
return java.util.Arrays.toString((double[]) o);
} else if (o instanceof char[]) {
return java.util.Arrays.toString((char[]) o);
} else {
return java.util.Objects.toString(o);
}
}

private static String normalizeString(String s) {
boolean isArray = (s.startsWith("{") && s.endsWith("}")) || (s.startsWith("[") && s.endsWith("]"));
if (isArray) {
s = s.replace(" ", "")
.replace("'", "")
.replace("\"", "")
.replace("{", "[")
.replace("}", "]");
}

return s;
}

private static void printCheck(String result, String expect, boolean ok) {
// System.out.println();
// System.out.println("--->");

System.out.println("result: " + result);
System.out.println("expect: " + expect);

System.out.println();
if (ok) {
System.out.println("比较结果为: 正确(right)");
} else {
System.out.println("比较结果为: 错误(error)");
}

// System.out.println();
// System.out.println("<---");
}

public static void runTestCases(Class<?> clazz) {
java.lang.reflect.Method[] methods = clazz.getDeclaredMethods();
java.util.Arrays.sort(methods, (o1, o2) -> {
return o1.getName().compareTo(o2.getName());
});
for (java.lang.reflect.Method method : methods) {
if (method.getName().startsWith("test")) {
System.out.println(String.format("--- %s --->", method.getName()));

Object object = null;
try {
object = method.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println(String.format("<--- %s ---", method.getName()));

if (object instanceof Boolean) {
Boolean ok = (Boolean) object;
if (!ok) {
break;
}
} else {
throw new RuntimeException(String.format("测试用例方法%s的返回值需要为布尔类型", method.getName()));
}
}
}
}

public static Object[] toArray(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0];
}

return list.toArray();
}

public static Object[][] toArrays(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0][0];
}

Object[][] arrays = new Object[list.size()][];
for (int i = 0; i < list.size(); i++) {
arrays[i] = toArray(list.get(i));
}

return arrays;
}

@SuppressWarnings("unchecked")
public static <T> T[] genArray(Class<T> clazz, int length) {
return (T[]) java.lang.reflect.Array.newInstance(clazz, length);
}

@SuppressWarnings("unchecked")
public static <T> T[][] genArrays(Class<T> clazz, int rows, int cols) {
T[] array = genArray(clazz, 0);

T[][] arrays = (T[][]) genArray(array.getClass(), rows);
for (int i = 0; i < arrays.length; i++) {
arrays[i] = genArray(clazz, cols);
}

return arrays;
}

public static <T> T[] toArray(java.util.List<T> list, Class<T> clazz) {
if (list == null || list.isEmpty()) {
return genArray(clazz, 0);
}

T[] array = genArray(clazz, list.size());
for (int i = 0; i < array.length; i++) {
array[i] = list.get(i);
}

return array;
}

public static <T> T[][] toArrays(java.util.List<java.util.List<T>> lists, Class<T> clazz) {
if (lists == null || lists.isEmpty()) {
return genArrays(clazz, 0, 0);
}

T[][] arrays = genArrays(clazz, lists.size(), 0);
for (int i = 0; i < lists.size(); i++) {
arrays[i] = toArray(lists.get(i), clazz);
}

return arrays;
}

public static <T> java.util.List<T> toList(T[] array) {
return java.util.Arrays.asList(array);
}

public static <T> java.util.List<java.util.List<T>> toLists(T[][] arrays) {
java.util.List<java.util.List<T>> lists = new java.util.ArrayList<>();
for (T[] array : arrays) {
lists.add(toList(array));
}

return lists;
}
}
}

旋转图像

题目描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。

你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。



示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[[7,4,1],[8,5,2],[9,6,3]]
示例 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]]


提示:

n == matrix.length == matrix[i].length
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000

题目解答

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
public class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length; // 行数和列数
float m = (n - 1) / 2.0f; // 原点

// 坐标正负性
// 右上区间,x为正,y为负
// 右下区间,x为正,y为正
// 左下区间,x为负,y为正
// 左上区间,x为负,y为负

// 旋转坐标时的符号变化总结
// 左上转到右上,旧x变新y符号不变,旧y变新x符号变
// 右上转到右下,旧x变新y符号不变,旧y变新x符号变
// 右下转到左下,旧x变新y符号不变,旧y变新x符号变
// 左下转到左上,旧x变新y符号不变,旧y变新x符号变
// 总结:新x等于旧y且符号变,新y等于旧x且符号不变

// 旋转坐标时的符号变化推导
// 说明 ox(old x)、nx(new x)、oy(old y)、ny(new y)
// 已知 ox = cos@、oy = sin@、cos90 = 0、sin90 = 1
// 所以 nx = cos(@ + 90) = cos@cos90 - sin@sin90 = -sin@ = -oy
// 所以 ny = sin(@ + 90) = sin@cos90 + cos@sin90 = cos@ = ox
// 总结:新x等于旧y且符号变,新y等于旧x且符号不变

int re = n / 2 - 1;
for (int i = 0; i <= re; i++) {
int ce = i + (n - 2 * (i + 1));
for (int j = i; j <= ce; j++) {
int r = i;
int c = j;

while (true) {
// 下标转坐标
float y = r - m;
float x = c - m;

// 旋转坐标
float z = x;
x = -y;
y = z;

// 坐标转下标
r = (int) (y + m);
c = (int) (x + m);

// 回到起点了,不能重复处理,结束
if (r == i && c == j) {
break;
}

// 交换当前点和第一个点的值
int v = matrix[r][c];
matrix[r][c] = matrix[i][j];
matrix[i][j] = v;
}
}
}
}
}

class TestMain {

public static void main(String[] args) {
TestUtils.runTestCases(TestCase.class);
}

public static class TestCase {

public static boolean testCase1() {
int[][] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};

new Solution().rotate(matrix);
int[][] result = matrix;
int[][] expect = {
{ 7, 4, 1 },
{ 8, 5, 2 },
{ 9, 6, 3 }
};

return TestUtils.check(result, expect);
}

public static boolean testCase2() {
int[][] matrix = {
{ 5, 1, 9, 11 },
{ 2, 4, 8, 10 },
{ 13, 3, 6, 7 },
{ 15, 14, 12, 16 }
};

new Solution().rotate(matrix);
int[][] result = matrix;
int[][] expect = {
{ 15, 13, 2, 5 },
{ 14, 3, 4, 1 },
{ 12, 6, 8, 9 },
{ 16, 7, 10, 11 }
};

return TestUtils.check(result, expect);
}
}

public static class TestUtils {

public static boolean check(Object result, Object expect) {
result = normalizeObject(result);
expect = normalizeObject(expect);
boolean ok = java.util.Objects.deepEquals(result, expect);
printCheck(toString(result), toString(expect), ok);
return ok;
}

public static boolean check(Object result, String expect) {
boolean ok = java.util.Objects.equals(normalizeString(toString(result)),
normalizeString(toString(expect)));
printCheck(normalizeString(toString(result)), normalizeString(toString(expect)), ok);
return ok;
}

private static Object normalizeObject(Object o) {
if (o instanceof java.util.List) {
java.util.List<?> list = (java.util.List<?>) o;
if (list != null && !list.isEmpty() && list.get(0) instanceof java.util.List) {
o = toArrays(list);
} else {
o = toArray(list);
}
}

return o;
}

private static String toString(Object o) {
if (o instanceof Object[]) {
return java.util.Arrays.deepToString((Object[]) o);
} else if (o instanceof int[]) {
return java.util.Arrays.toString((int[]) o);
} else if (o instanceof double[]) {
return java.util.Arrays.toString((double[]) o);
} else if (o instanceof char[]) {
return java.util.Arrays.toString((char[]) o);
} else {
return java.util.Objects.toString(o);
}
}

private static String normalizeString(String s) {
boolean isArray = (s.startsWith("{") && s.endsWith("}")) || (s.startsWith("[") && s.endsWith("]"));
if (isArray) {
s = s.replace(" ", "")
.replace("'", "")
.replace("\"", "")
.replace("{", "[")
.replace("}", "]");
}

return s;
}

private static void printCheck(String result, String expect, boolean ok) {
// System.out.println();
// System.out.println("--->");

System.out.println("result: " + result);
System.out.println("expect: " + expect);

System.out.println();
if (ok) {
System.out.println("比较结果为: 正确(right)");
} else {
System.out.println("比较结果为: 错误(error)");
}

// System.out.println();
// System.out.println("<---");
}

public static void runTestCases(Class<?> clazz) {
java.lang.reflect.Method[] methods = clazz.getDeclaredMethods();
java.util.Arrays.sort(methods, (o1, o2) -> {
return o1.getName().compareTo(o2.getName());
});
for (java.lang.reflect.Method method : methods) {
if (method.getName().startsWith("test")) {
System.out.println(String.format("--- %s --->", method.getName()));

Object object = null;
try {
object = method.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println(String.format("<--- %s ---", method.getName()));

if (object instanceof Boolean) {
Boolean ok = (Boolean) object;
if (!ok) {
break;
}
} else {
throw new RuntimeException(String.format("测试用例方法%s的返回值需要为布尔类型", method.getName()));
}
}
}
}

public static Object[] toArray(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0];
}

return list.toArray();
}

public static Object[][] toArrays(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0][0];
}

Object[][] arrays = new Object[list.size()][];
for (int i = 0; i < list.size(); i++) {
arrays[i] = toArray(list.get(i));
}

return arrays;
}

@SuppressWarnings("unchecked")
public static <T> T[] genArray(Class<T> clazz, int length) {
return (T[]) java.lang.reflect.Array.newInstance(clazz, length);
}

@SuppressWarnings("unchecked")
public static <T> T[][] genArrays(Class<T> clazz, int rows, int cols) {
T[] array = genArray(clazz, 0);

T[][] arrays = (T[][]) genArray(array.getClass(), rows);
for (int i = 0; i < arrays.length; i++) {
arrays[i] = genArray(clazz, cols);
}

return arrays;
}

public static <T> T[] toArray(java.util.List<T> list, Class<T> clazz) {
if (list == null || list.isEmpty()) {
return genArray(clazz, 0);
}

T[] array = genArray(clazz, list.size());
for (int i = 0; i < array.length; i++) {
array[i] = list.get(i);
}

return array;
}

public static <T> T[][] toArrays(java.util.List<java.util.List<T>> lists, Class<T> clazz) {
if (lists == null || lists.isEmpty()) {
return genArrays(clazz, 0, 0);
}

T[][] arrays = genArrays(clazz, lists.size(), 0);
for (int i = 0; i < lists.size(); i++) {
arrays[i] = toArray(lists.get(i), clazz);
}

return arrays;
}

public static <T> java.util.List<T> toList(T[] array) {
return java.util.Arrays.asList(array);
}

public static <T> java.util.List<java.util.List<T>> toLists(T[][] arrays) {
java.util.List<java.util.List<T>> lists = new java.util.ArrayList<>();
for (T[] array : arrays) {
lists.add(toList(array));
}

return lists;
}
}
}

矩阵置零

题目描述

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
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。



示例 1:

输入:matrix = [[1,1,1],[1,0,1],[1,1,1]]
输出:[[1,0,1],[0,0,0],[1,0,1]]
示例 2:

输入:matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
输出:[[0,0,0,0],[0,4,5,0],[0,3,1,0]]


提示:

m == matrix.length
n == matrix[0].length
1 <= m, n <= 200
-231 <= matrix[i][j] <= 231 - 1


进阶:

一个直观的解决方案是使用 O(mn) 的额外空间,但这并不是一个好的解决方案。
一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
你能想出一个仅使用常量空间的解决方案吗?

题目解答

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
public class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length; // 行数
int n = matrix[0].length;// 列数

boolean[] rows = new boolean[m]; // 标记每一行中是否存在0
boolean[] cols = new boolean[n]; // 标记每一列中是否存在0

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
if (!rows[i]) {
rows[i] = true;
}
if (!cols[j]) {
cols[j] = true;
}
}
}
}

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (rows[i] || cols[j]) {
matrix[i][j] = 0;
}
}
}
}
}

class TestMain {

public static void main(String[] args) {
TestUtils.runTestCases(TestCase.class);
}

public static class TestCase {

public static boolean testCase1() {
int[][] matrix = {
{ 1, 1, 1 },
{ 1, 0, 1 },
{ 1, 1, 1 }
};

new Solution().setZeroes(matrix);
int[][] result = matrix;
int[][] expect = {
{ 1, 0, 1 },
{ 0, 0, 0 },
{ 1, 0, 1 }
};

return TestUtils.check(result, expect);
}

public static boolean testCase2() {
int[][] matrix = {
{ 0, 1, 2, 0 },
{ 3, 4, 5, 2 },
{ 1, 3, 1, 5 }
};

new Solution().setZeroes(matrix);
int[][] result = matrix;
int[][] expect = {
{ 0, 0, 0, 0 },
{ 0, 4, 5, 0 },
{ 0, 3, 1, 0 }
};

return TestUtils.check(result, expect);
}

public static boolean testCase3() {
int[][] matrix = {
{ -2147483648 },
{ 2 },
{ 3 }
};

new Solution().setZeroes(matrix);
int[][] result = matrix;
int[][] expect = {
{ -2147483648 },
{ 2 },
{ 3 }
};

return TestUtils.check(result, expect);
}
}

public static class TestUtils {

public static boolean check(Object result, Object expect) {
result = normalizeObject(result);
expect = normalizeObject(expect);
boolean ok = java.util.Objects.deepEquals(result, expect);
printCheck(toString(result), toString(expect), ok);
return ok;
}

public static boolean check(Object result, String expect) {
boolean ok = java.util.Objects.equals(normalizeString(toString(result)),
normalizeString(toString(expect)));
printCheck(normalizeString(toString(result)), normalizeString(toString(expect)), ok);
return ok;
}

private static Object normalizeObject(Object o) {
if (o instanceof java.util.List) {
java.util.List<?> list = (java.util.List<?>) o;
if (list != null && !list.isEmpty() && list.get(0) instanceof java.util.List) {
o = toArrays(list);
} else {
o = toArray(list);
}
}

return o;
}

private static String toString(Object o) {
if (o instanceof Object[]) {
return java.util.Arrays.deepToString((Object[]) o);
} else if (o instanceof int[]) {
return java.util.Arrays.toString((int[]) o);
} else if (o instanceof double[]) {
return java.util.Arrays.toString((double[]) o);
} else if (o instanceof char[]) {
return java.util.Arrays.toString((char[]) o);
} else {
return java.util.Objects.toString(o);
}
}

private static String normalizeString(String s) {
boolean isArray = (s.startsWith("{") && s.endsWith("}")) || (s.startsWith("[") && s.endsWith("]"));
if (isArray) {
s = s.replace(" ", "")
.replace("'", "")
.replace("\"", "")
.replace("{", "[")
.replace("}", "]");
}

return s;
}

private static void printCheck(String result, String expect, boolean ok) {
// System.out.println();
// System.out.println("--->");

System.out.println("result: " + result);
System.out.println("expect: " + expect);

System.out.println();
if (ok) {
System.out.println("比较结果为: 正确(right)");
} else {
System.out.println("比较结果为: 错误(error)");
}

// System.out.println();
// System.out.println("<---");
}

public static void runTestCases(Class<?> clazz) {
java.lang.reflect.Method[] methods = clazz.getDeclaredMethods();
java.util.Arrays.sort(methods, (o1, o2) -> {
return o1.getName().compareTo(o2.getName());
});
for (java.lang.reflect.Method method : methods) {
if (method.getName().startsWith("test")) {
System.out.println(String.format("--- %s --->", method.getName()));

Object object = null;
try {
object = method.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println(String.format("<--- %s ---", method.getName()));

if (object instanceof Boolean) {
Boolean ok = (Boolean) object;
if (!ok) {
break;
}
} else {
throw new RuntimeException(String.format("测试用例方法%s的返回值需要为布尔类型", method.getName()));
}
}
}
}

public static Object[] toArray(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0];
}

return list.toArray();
}

public static Object[][] toArrays(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0][0];
}

Object[][] arrays = new Object[list.size()][];
for (int i = 0; i < list.size(); i++) {
arrays[i] = toArray(list.get(i));
}

return arrays;
}

@SuppressWarnings("unchecked")
public static <T> T[] genArray(Class<T> clazz, int length) {
return (T[]) java.lang.reflect.Array.newInstance(clazz, length);
}

@SuppressWarnings("unchecked")
public static <T> T[][] genArrays(Class<T> clazz, int rows, int cols) {
T[] array = genArray(clazz, 0);

T[][] arrays = (T[][]) genArray(array.getClass(), rows);
for (int i = 0; i < arrays.length; i++) {
arrays[i] = genArray(clazz, cols);
}

return arrays;
}

public static <T> T[] toArray(java.util.List<T> list, Class<T> clazz) {
if (list == null || list.isEmpty()) {
return genArray(clazz, 0);
}

T[] array = genArray(clazz, list.size());
for (int i = 0; i < array.length; i++) {
array[i] = list.get(i);
}

return array;
}

public static <T> T[][] toArrays(java.util.List<java.util.List<T>> lists, Class<T> clazz) {
if (lists == null || lists.isEmpty()) {
return genArrays(clazz, 0, 0);
}

T[][] arrays = genArrays(clazz, lists.size(), 0);
for (int i = 0; i < lists.size(); i++) {
arrays[i] = toArray(lists.get(i), clazz);
}

return arrays;
}

public static <T> java.util.List<T> toList(T[] array) {
return java.util.Arrays.asList(array);
}

public static <T> java.util.List<java.util.List<T>> toLists(T[][] arrays) {
java.util.List<java.util.List<T>> lists = new java.util.ArrayList<>();
for (T[] array : arrays) {
lists.add(toList(array));
}

return lists;
}
}
}

进阶解法

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
public class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length; // 行数
int n = matrix[0].length;// 列数

boolean r0 = false; // 标记第0行是否存在0
boolean c0 = false; // 标记第0列是否存在0

for (int j = 0; j < n; j++) {
if (matrix[0][j] == 0) {
r0 = true;
break;
}
}
for (int i = 0; i < m; i++) {
if (matrix[i][0] == 0) {
c0 = true;
break;
}
}

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
if (matrix[0][j] != 0) {
matrix[0][j] = 0;
}

if (matrix[i][0] != 0) {
matrix[i][0] = 0;
}
}
}
}

for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][j] != 0) {
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
matrix[i][j] = 0;
}
}
}
}

// 处理第0行
if (r0) {
for (int j = 0; j < n; j++) {
if (matrix[0][j] != 0) {
matrix[0][j] = 0;
}
}
}

// 处理第0列
if (c0) {
for (int i = 0; i < m; i++) {
if (matrix[i][0] != 0) {
matrix[i][0] = 0;
}
}
}
}
}

class TestMain {

public static void main(String[] args) {
TestUtils.runTestCases(TestCase.class);
}

public static class TestCase {

public static boolean testCase1() {
int[][] matrix = {
{ 1, 1, 1 },
{ 1, 0, 1 },
{ 1, 1, 1 }
};

new Solution().setZeroes(matrix);
int[][] result = matrix;
int[][] expect = {
{ 1, 0, 1 },
{ 0, 0, 0 },
{ 1, 0, 1 }
};

return TestUtils.check(result, expect);
}

public static boolean testCase2() {
int[][] matrix = {
{ 0, 1, 2, 0 },
{ 3, 4, 5, 2 },
{ 1, 3, 1, 5 }
};

new Solution().setZeroes(matrix);
int[][] result = matrix;
int[][] expect = {
{ 0, 0, 0, 0 },
{ 0, 4, 5, 0 },
{ 0, 3, 1, 0 }
};

return TestUtils.check(result, expect);
}

public static boolean testCase3() {
int[][] matrix = {
{ -2147483648 },
{ 2 },
{ 3 }
};

new Solution().setZeroes(matrix);
int[][] result = matrix;
int[][] expect = {
{ -2147483648 },
{ 2 },
{ 3 }
};

return TestUtils.check(result, expect);
}
}

public static class TestUtils {

public static boolean check(Object result, Object expect) {
result = normalizeObject(result);
expect = normalizeObject(expect);
boolean ok = java.util.Objects.deepEquals(result, expect);
printCheck(toString(result), toString(expect), ok);
return ok;
}

public static boolean check(Object result, String expect) {
boolean ok = java.util.Objects.equals(normalizeString(toString(result)),
normalizeString(toString(expect)));
printCheck(normalizeString(toString(result)), normalizeString(toString(expect)), ok);
return ok;
}

private static Object normalizeObject(Object o) {
if (o instanceof java.util.List) {
java.util.List<?> list = (java.util.List<?>) o;
if (list != null && !list.isEmpty() && list.get(0) instanceof java.util.List) {
o = toArrays(list);
} else {
o = toArray(list);
}
}

return o;
}

private static String toString(Object o) {
if (o instanceof Object[]) {
return java.util.Arrays.deepToString((Object[]) o);
} else if (o instanceof int[]) {
return java.util.Arrays.toString((int[]) o);
} else if (o instanceof double[]) {
return java.util.Arrays.toString((double[]) o);
} else if (o instanceof char[]) {
return java.util.Arrays.toString((char[]) o);
} else {
return java.util.Objects.toString(o);
}
}

private static String normalizeString(String s) {
boolean isArray = (s.startsWith("{") && s.endsWith("}")) || (s.startsWith("[") && s.endsWith("]"));
if (isArray) {
s = s.replace(" ", "")
.replace("'", "")
.replace("\"", "")
.replace("{", "[")
.replace("}", "]");
}

return s;
}

private static void printCheck(String result, String expect, boolean ok) {
// System.out.println();
// System.out.println("--->");

System.out.println("result: " + result);
System.out.println("expect: " + expect);

System.out.println();
if (ok) {
System.out.println("比较结果为: 正确(right)");
} else {
System.out.println("比较结果为: 错误(error)");
}

// System.out.println();
// System.out.println("<---");
}

public static void runTestCases(Class<?> clazz) {
java.lang.reflect.Method[] methods = clazz.getDeclaredMethods();
java.util.Arrays.sort(methods, (o1, o2) -> {
return o1.getName().compareTo(o2.getName());
});
for (java.lang.reflect.Method method : methods) {
if (method.getName().startsWith("test")) {
System.out.println(String.format("--- %s --->", method.getName()));

Object object = null;
try {
object = method.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println(String.format("<--- %s ---", method.getName()));

if (object instanceof Boolean) {
Boolean ok = (Boolean) object;
if (!ok) {
break;
}
} else {
throw new RuntimeException(String.format("测试用例方法%s的返回值需要为布尔类型", method.getName()));
}
}
}
}

public static Object[] toArray(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0];
}

return list.toArray();
}

public static Object[][] toArrays(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0][0];
}

Object[][] arrays = new Object[list.size()][];
for (int i = 0; i < list.size(); i++) {
arrays[i] = toArray(list.get(i));
}

return arrays;
}

@SuppressWarnings("unchecked")
public static <T> T[] genArray(Class<T> clazz, int length) {
return (T[]) java.lang.reflect.Array.newInstance(clazz, length);
}

@SuppressWarnings("unchecked")
public static <T> T[][] genArrays(Class<T> clazz, int rows, int cols) {
T[] array = genArray(clazz, 0);

T[][] arrays = (T[][]) genArray(array.getClass(), rows);
for (int i = 0; i < arrays.length; i++) {
arrays[i] = genArray(clazz, cols);
}

return arrays;
}

public static <T> T[] toArray(java.util.List<T> list, Class<T> clazz) {
if (list == null || list.isEmpty()) {
return genArray(clazz, 0);
}

T[] array = genArray(clazz, list.size());
for (int i = 0; i < array.length; i++) {
array[i] = list.get(i);
}

return array;
}

public static <T> T[][] toArrays(java.util.List<java.util.List<T>> lists, Class<T> clazz) {
if (lists == null || lists.isEmpty()) {
return genArrays(clazz, 0, 0);
}

T[][] arrays = genArrays(clazz, lists.size(), 0);
for (int i = 0; i < lists.size(); i++) {
arrays[i] = toArray(lists.get(i), clazz);
}

return arrays;
}

public static <T> java.util.List<T> toList(T[] array) {
return java.util.Arrays.asList(array);
}

public static <T> java.util.List<java.util.List<T>> toLists(T[][] arrays) {
java.util.List<java.util.List<T>> lists = new java.util.ArrayList<>();
for (T[] array : arrays) {
lists.add(toList(array));
}

return lists;
}
}
}

生命游戏

题目描述

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
根据 百度百科 , 生命游戏 ,简称为 生命 ,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机。

给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态: 1 即为 活细胞 (live),或 0 即为 死细胞 (dead)。每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律:

如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
如果死细胞周围正好有三个活细胞,则该位置死细胞复活;
下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是同时发生的。给你 m x n 网格面板 board 的当前状态,返回下一个状态。



示例 1:

输入:board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
输出:[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
示例 2:

输入:board = [[1,1],[1,0]]
输出:[[1,1],[1,1]]


提示:

m == board.length
n == board[i].length
1 <= m, n <= 25
board[i][j] 为 0 或 1


进阶:

你可以使用原地算法解决本题吗?请注意,面板上所有格子需要同时被更新:你不能先更新某些格子,然后使用它们的更新后的值再更新其他格子。
本题中,我们使用二维数组来表示面板。原则上,面板是无限的,但当活细胞侵占了面板边界时会造成问题。你将如何解决这些问题?

题目解答

除了原有的0和1状态,还添加了2和3来记录原有状态的变化

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
public class Solution {
public void gameOfLife(int[][] board) {
int m = board.length; // 行数
int n = board[0].length;// 列数

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int count = countAlives(board, i, j);

if (board[i][j] == 1) {
if (count < 2 || count > 3) {
// 3代表过去是活的,现在死了
board[i][j] = 3;
}
} else {
if (count == 3) {
// 2代表过去是死的,现在活了
board[i][j] = 2;
}
}
}
}

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 2) {
board[i][j] = 1;
} else if (board[i][j] == 3) {
board[i][j] = 0;
}
}
}
}

private int countAlives(int[][] board, int i, int j) {
int count = 0;

count = count + countAlive(board, i - 1, j - 1);
count = count + countAlive(board, i - 1, j);
count = count + countAlive(board, i - 1, j + 1);
count = count + countAlive(board, i, j - 1);
count = count + countAlive(board, i, j + 1);
count = count + countAlive(board, i + 1, j - 1);
count = count + countAlive(board, i + 1, j);
count = count + countAlive(board, i + 1, j + 1);

return count;
}

private int countAlive(int[][] board, int i, int j) {
int m = board.length; // 行数
int n = board[0].length;// 列数

if (i < 0 || j < 0 || i >= m || j >= n) {
return 0;
}

int v = board[i][j];

// 3代表过去是活的,现在死了
// 当前为3时说明原来为1,即原来是活的
if (v == 1 || v == 3) {
return 1;
}

return 0;
}
}

class TestMain {

public static void main(String[] args) {
TestUtils.runTestCases(TestCase.class);
}

public static class TestCase {

public static boolean testCase1() {
int[][] board = {
{ 0, 1, 0 },
{ 0, 0, 1 },
{ 1, 1, 1 },
{ 0, 0, 0 }
};

new Solution().gameOfLife(board);
int[][] result = board;
int[][] expect = {
{ 0, 0, 0 },
{ 1, 0, 1 },
{ 0, 1, 1 },
{ 0, 1, 0 }
};

return TestUtils.check(result, expect);
}

public static boolean testCase2() {
int[][] board = {
{ 1, 1 },
{ 1, 0 }
};

new Solution().gameOfLife(board);
int[][] result = board;
int[][] expect = {
{ 1, 1 },
{ 1, 1 }
};

return TestUtils.check(result, expect);
}
}

public static class TestUtils {

public static boolean check(Object result, Object expect) {
result = normalizeObject(result);
expect = normalizeObject(expect);
boolean ok = java.util.Objects.deepEquals(result, expect);
printCheck(toString(result), toString(expect), ok);
return ok;
}

public static boolean check(Object result, String expect) {
boolean ok = java.util.Objects.equals(normalizeString(toString(result)),
normalizeString(toString(expect)));
printCheck(normalizeString(toString(result)), normalizeString(toString(expect)), ok);
return ok;
}

private static Object normalizeObject(Object o) {
if (o instanceof java.util.List) {
java.util.List<?> list = (java.util.List<?>) o;
if (list != null && !list.isEmpty() && list.get(0) instanceof java.util.List) {
o = toArrays(list);
} else {
o = toArray(list);
}
}

return o;
}

private static String toString(Object o) {
if (o instanceof Object[]) {
return java.util.Arrays.deepToString((Object[]) o);
} else if (o instanceof int[]) {
return java.util.Arrays.toString((int[]) o);
} else if (o instanceof double[]) {
return java.util.Arrays.toString((double[]) o);
} else if (o instanceof char[]) {
return java.util.Arrays.toString((char[]) o);
} else {
return java.util.Objects.toString(o);
}
}

private static String normalizeString(String s) {
boolean isArray = (s.startsWith("{") && s.endsWith("}")) || (s.startsWith("[") && s.endsWith("]"));
if (isArray) {
s = s.replace(" ", "")
.replace("'", "")
.replace("\"", "")
.replace("{", "[")
.replace("}", "]");
}

return s;
}

private static void printCheck(String result, String expect, boolean ok) {
// System.out.println();
// System.out.println("--->");

System.out.println("result: " + result);
System.out.println("expect: " + expect);

System.out.println();
if (ok) {
System.out.println("比较结果为: 正确(right)");
} else {
System.out.println("比较结果为: 错误(error)");
}

// System.out.println();
// System.out.println("<---");
}

public static void runTestCases(Class<?> clazz) {
java.lang.reflect.Method[] methods = clazz.getDeclaredMethods();
java.util.Arrays.sort(methods, (o1, o2) -> {
return o1.getName().compareTo(o2.getName());
});
for (java.lang.reflect.Method method : methods) {
if (method.getName().startsWith("test")) {
System.out.println(String.format("--- %s --->", method.getName()));

Object object = null;
try {
object = method.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println(String.format("<--- %s ---", method.getName()));

if (object instanceof Boolean) {
Boolean ok = (Boolean) object;
if (!ok) {
break;
}
} else {
throw new RuntimeException(String.format("测试用例方法%s的返回值需要为布尔类型", method.getName()));
}
}
}
}

public static Object[] toArray(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0];
}

return list.toArray();
}

public static Object[][] toArrays(Object o) {
java.util.List<?> list = (java.util.List<?>) o;
if (list == null || list.isEmpty()) {
return new Object[0][0];
}

Object[][] arrays = new Object[list.size()][];
for (int i = 0; i < list.size(); i++) {
arrays[i] = toArray(list.get(i));
}

return arrays;
}

@SuppressWarnings("unchecked")
public static <T> T[] genArray(Class<T> clazz, int length) {
return (T[]) java.lang.reflect.Array.newInstance(clazz, length);
}

@SuppressWarnings("unchecked")
public static <T> T[][] genArrays(Class<T> clazz, int rows, int cols) {
T[] array = genArray(clazz, 0);

T[][] arrays = (T[][]) genArray(array.getClass(), rows);
for (int i = 0; i < arrays.length; i++) {
arrays[i] = genArray(clazz, cols);
}

return arrays;
}

public static <T> T[] toArray(java.util.List<T> list, Class<T> clazz) {
if (list == null || list.isEmpty()) {
return genArray(clazz, 0);
}

T[] array = genArray(clazz, list.size());
for (int i = 0; i < array.length; i++) {
array[i] = list.get(i);
}

return array;
}

public static <T> T[][] toArrays(java.util.List<java.util.List<T>> lists, Class<T> clazz) {
if (lists == null || lists.isEmpty()) {
return genArrays(clazz, 0, 0);
}

T[][] arrays = genArrays(clazz, lists.size(), 0);
for (int i = 0; i < lists.size(); i++) {
arrays[i] = toArray(lists.get(i), clazz);
}

return arrays;
}

public static <T> java.util.List<T> toList(T[] array) {
return java.util.Arrays.asList(array);
}

public static <T> java.util.List<java.util.List<T>> toLists(T[][] arrays) {
java.util.List<java.util.List<T>> lists = new java.util.ArrayList<>();
for (T[] array : arrays) {
lists.add(toList(array));
}

return lists;
}
}
}

困难

只想买包辣条