-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathValid Sudoku.java
36 lines (29 loc) · 1.01 KB
/
Valid Sudoku.java
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
public class Solution {
/**
* Time: O(n^3)
* Memory: O(1)
*/
public boolean isValidSudoku(char[][] board) {
for (int i = 0; i < SIZE * SIZE; ++i) {
for (int j = 0; j < SIZE * SIZE; ++j) {
if (board[i][j] == EMPTY)
continue;
for (int k = 0; k < SIZE * SIZE; ++k) {
if (board[i][k] == board[i][j] && k != j)
return false;
if (board[k][j] == board[i][j] && k != i)
return false;
}
int row = i - i % SIZE;
int col = j - j % SIZE;
for (int r = row; r < row + SIZE; ++r)
for (int c = col; c < col + SIZE; ++c)
if (board[r][c] == board[i][j] && (r != i || c != j))
return false;
}
}
return true;
}
private static final int SIZE = 3;
private static final char EMPTY = '.';
}