-
Notifications
You must be signed in to change notification settings - Fork 5
/
IsValidSudoku.java
80 lines (70 loc) · 2.12 KB
/
IsValidSudoku.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*https://leetcode.com/problems/valid-sudoku/*/
class Solution
{
public boolean isSafe(char[][] b, int row, int col, char key)
{
//the row should not contain duplicates
for (int i = 0; i < 9; ++i)
if (b[row][i] == key && i != col)
return false;
//the column should not contain duplicates
for (int i = 0; i < 9; ++i)
if (b[i][col] == key && i != row)
return false;
//the 3x3 box should not contain duplicates
int sqrt = (int)Math.sqrt(b.length);
int boxRowStart = row - row % sqrt;
int boxColStart = col - col % sqrt;
for (int r = boxRowStart; r < boxRowStart + sqrt; r++)
for (int d = boxColStart; d < boxColStart + sqrt; d++)
if (b[r][d] == key && !(r == row && d == col))
return false;
return true;
}
public boolean isValidSudoku(char[][] board)
{
//check if the sudoku is already blank and mark it
boolean isEmpty = true;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (board[i][j] != '.')
{
isEmpty = false;
break;
}
}
if (!isEmpty)
break;
}
//if blank then there always exists a solution
if (isEmpty) return true;
int i = 0, j = 0;
//for all cells
while (i < 9 && j < 9)
{
//if the cell is empty skip it
if (board[i][j] == '.')
{
++j;
if (j == 9)
{
j = i == 8 ? 9 : 0;
++i;
}
continue;
}
//check the safety of the cell
if (!isSafe(board,i,j,board[i][j])) return false;
//move to next cell
++j;
if (j == 9)
{
j = i == 8 ? 9 : 0;
++i;
}
}
return true;
}
}