forked from yuanx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidSudoku.java
More file actions
43 lines (37 loc) · 884 Bytes
/
ValidSudoku.java
File metadata and controls
43 lines (37 loc) · 884 Bytes
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
public class Solution {
public boolean isValidSudoku(char[][] board) {
// Start typing your Java solution below
// DO NOT write main() function
for(int i=0; i<9; i++)
for(int j=0; j<9; j++){
if(board[i][j] == '.')
continue;
else{
char temp = board[i][j];
board[i][j] = 'C';
if(!isvalid(board, i, j, temp-'0')){
board[i][j]=temp;
return false;
}
else
board[i][j]=temp;
}
}
return true;
}
public boolean isvalid(char[][] board, int x, int y, int t){
char temp = (char)('0'+t);
for(int i=0; i<9; i++){
if(board[x][i] == temp || board[i][y] == temp)
return false;
}
int bx = x/3;
int by = y/3;
for(int p=bx*3; p<bx*3+3; p++)
for(int q=by*3; q<by*3+3; q++){
if(board[p][q]== temp)
return false;
}
return true;
}
}