-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode_00036.java
More file actions
47 lines (44 loc) · 1.67 KB
/
LeetCode_00036.java
File metadata and controls
47 lines (44 loc) · 1.67 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
package com.github.jerring.leetcode;
public class LeetCode_00036 {
// public boolean isValidSudoku(char[][] board) {
// boolean[][] row = new boolean[9][9]; // 代表每一行
// boolean[][] col = new boolean[9][9]; // 代表每一列
// boolean[][] block = new boolean[9][9]; // 代表每个 3x3 宫格
// for (int i = 0; i < 9; ++i) {
// for (int j = 0; j < 9; ++j) {
// if (board[i][j] != '.') {
// int t = board[i][j] - '1';
// int k = i / 3 * 3 + j / 3;
// if (row[i][t] || col[j][t] || block[k][t]) {
// return false;
// }
// row[i][t] = true;
// col[j][t] = true;
// block[k][t] = true;
// }
// }
// }
// return true;
// }
public boolean isValidSudoku(char[][] board) {
int[] row = new int[9]; // 代表每一行
int[] col = new int[9]; // 代表每一列
int[] block = new int[9]; // 代表每个 3x3 宫格
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
if (board[i][j] != '.') {
// 映射到对应的二进制位
int t = 1 << (board[i][j] - '1');
int k = i / 3 * 3 + j / 3;
if ((row[i] & t) != 0 || (col[j] & t) != 0 || (block[k] & t) != 0) {
return false;
}
row[i] |= t;
col[j] |= t;
block[k] |= t;
}
}
}
return true;
}
}