-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
34 lines (31 loc) · 1.06 KB
/
Solution.java
File metadata and controls
34 lines (31 loc) · 1.06 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
package leetCode_36;
/**
* @author dimdark
*/
public class Solution {
public boolean isValidSudoku(char[][] board) {
if (board == null || board.length != 9 || board[0].length != 9) {
return false;
}
boolean[][] rowValids = new boolean[9][9];
boolean[][] columnValids = new boolean[9][9];
boolean[][] subBoxValids = new boolean[9][9];
for (int i = 0; i < board.length; ++i) {
for (int j = 0; j < board[0].length; ++j) {
if (board[i][j] == '.') {
continue;
}
if (!Character.isDigit(board[i][j])) {
return false;
}
int num = board[i][j] - '0' - 1;
int k = i / 3 * 3 + j / 3; // beautiful
if (rowValids[i][num] || columnValids[j][num] || subBoxValids[k][num]) {
return false;
}
rowValids[i][num] = columnValids[j][num] = subBoxValids[k][num] = true;
}
}
return true;
}
}