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) {
for(int i=0;i<9;i++){
if(!isNineValid(board[i])){
return false;
}
/** 竖直 */
char[] tem=new char[9];
for(int i1=0;i1<9;i1++){
tem[i1]=board[i1][i];
}
if(!isNineValid(tem)){
return false;
}
/** 方块 */
int xx=i/3*3,yy=i%3*3;
for(int i1=0;i1<9;i1++){
tem[i1]=board[xx+i1/3][yy+i1%3];
}
if(!isNineValid(tem)){
return false;
}
}
return true;
}
boolean isNineValid(char[] target){
int[] flag=new int[9];
for(char i:target){
if(i=='.')continue;
flag[i-'1']++;
}
for(int i:flag){
if(i>1)return false;
}
return true;
}
}