Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
//分三次判断
public class Solution {
public boolean isValidSudoku(char[][] board) {
boolean[] nine=new boolean[9];
for(int i=0;i<9;i++){
Arrays.fill(nine, false);
for(int j=0;j<9;j++){
if(!valid(nine,board[i][j])){return false;}
}
}
for(int i=0;i<9;i++){
Arrays.fill(nine, false);
for(int j=0;j<9;j++){
if(!valid(nine,board[j][i])){return false;}
}
}
for(int i=0;i<9;i+=3){
for(int j=0;j<9;j+=3){
Arrays.fill(nine, false);
for(int k=0;k<9;k++){
if(!valid(nine,board[i+k/3][j+k%3])){return false;}
}
}
}
return true;
}
public boolean valid(boolean []arr,char target){
if(target=='.'){return true;}
int num=target-'0';
if(num<1||num>9||arr[num-1]){return false;}
arr[num-1]=true;
return true;
}
}