forked from algorithm024/algorithm024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidSudoku.java
More file actions
31 lines (30 loc) · 901 Bytes
/
ValidSudoku.java
File metadata and controls
31 lines (30 loc) · 901 Bytes
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
/**
* 36. 有效的数独
* https://leetcode-cn.com/problems/valid-sudoku/
*
*
*/
class Solution {
public boolean isValidSudoku(char[][] board) {
// 记录行
boolean[][] row = new boolean[9][9];
boolean[][] col = new boolean[9][9];
boolean[][] box = new boolean[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int num = board[i][j] - '1';
int block = i / 3 * 3 + j / 3;
if (row[i][num] || col[j][num] || box[block][num]) {
return false;
}else {
row[i][num] = true;
col[j][num] = true;
box[block][num] = true;
}
}
}
}
return true;
}
}