-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_sudoku_lc36.java
50 lines (47 loc) · 1.48 KB
/
valid_sudoku_lc36.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public boolean isValidSudoku(char[][] board) {
Set<Character> uniqNum = new HashSet<>();
// check all rows for duplicacy
for (int i=0; i<9; i++)
{
uniqNum.clear();
for (int j=0; j<9; j++)
{
if (uniqNum.contains(board[i][j])) return false;
if(board[i][j] != '.') uniqNum.add(board[i][j]);
}
}
// check all columns for duplicacy
for (int j=0; j<9; j++)
{
uniqNum.clear();
for (int i=0; i<9; i++)
{
if (uniqNum.contains(board[i][j])) return false;
if(board[i][j] != '.') uniqNum.add(board[i][j]);
}
}
// check sub-sudokus
// int rowB=0, rowE=3, colB=0, colE=3;
for (int rowB=0; rowB<9; rowB += 3)
{
for (int colB=0; colB<9; colB += 3)
{
uniqNum.clear();
for (int i=rowB; i<rowB+3; i++)
{
for (int j=colB; j<colB+3; j++)
{
if (uniqNum.contains(board[i][j])) return false;
if(board[i][j] != '.') uniqNum.add(board[i][j]);
}
}
// colB = colE;
// colE *= (colT+1);
}
// rowB = rowE;
// rowE *= (rowT+1);
}
return true;
}
}