-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidSudoku.java
More file actions
53 lines (50 loc) · 1.98 KB
/
ValidSudoku.java
File metadata and controls
53 lines (50 loc) · 1.98 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
/*
* https://leetcode.com/problems/valid-sudoku/
*/
public class ValidSudoku {
public boolean isValidSudoku(char[][] board) {
boolean[][] box = new boolean[9][9];
boolean[][] row = new boolean[9][9];
boolean[][] column = new boolean[9][9];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
char c = board[i][j];
if (c != '.') {
int digitMinusOne = c - '1';
if (row[i][digitMinusOne]) {
return false;
}
row[i][digitMinusOne] = true;
if (column[j][digitMinusOne]) {
return false;
}
column[j][digitMinusOne] = true;
int boxNumber = getBoxNumber(i, j);
if (box[boxNumber][digitMinusOne]) {
return false;
}
box[boxNumber][digitMinusOne] = true;
}
}
}
return true;
}
private int getBoxNumber(int i, int j) {
return i / 3 + j - j % 3;
}
public static void main(String[] args) {
System.out.println(new ValidSudoku().isValidSudoku(
new char[][]{
{'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
}
}