forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path37_sudokuSolver.java
36 lines (33 loc) · 1.04 KB
/
37_sudokuSolver.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 {
public void solveSudoku(char[][] board) {
solve(board);
}
public boolean solve(char[][] board){
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
if(board[i][j] == '.'){
for(char k = '1'; k <= '9'; k++){
if(isValid(board,i,j,k)){
board[i][j] = k;
if(solve(board)) return true;
else board[i][j] = '.';
}
}
return false;
}
}
}
return true;
}
public boolean isValid(char[][] board, int i, int j, char c){
for(int k = 0; k < 9; k++){
if(board[i][k] == c || board[k][j] == c) return false;
}
for(int x= i/3*3; x< i/3*3+3; x++){
for(int y = j/3*3; y < j/3*3+3; y++){
if(board[x][y] == c) return false;
}
}
return true;
}
}