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
'.'
.
只需要判断是否有效,不需要解出来。
public class Solution {
public boolean isValidSudoku(char[][] board) {
int len = board.length;
for(int i=0;i<len;i++){
HashSet<Character> ss1 = new HashSet<Character>();
HashSet<Character> ss2 = new HashSet<Character>();
HashSet<Character> ss3 = new HashSet<Character>();
for(int j=0;j<len;j++){
if(board[i][j]!='.'){
if(!ss1.contains(board[i][j])){
ss1.add(board[i][j]);
}else{
return false;
}}
if(board[j][i]!='.'){
if(!ss2.contains(board[j][i])){z
ss2.add(board[j][i]);
}else{
return false;
}}
if(board[j/3+i/3*3][j%3+i%3*3]!='.'){
if(!ss3.contains(board[j/3+i/3*3][j%3+i%3*3])){
ss3.add(board[j/3+i/3*3][j%3+i%3*3]);
}else{
return false;
}}
}
}
return true;
}
}