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.
public class Solution {
public boolean isValidSudoku(char[][] board)
{
int[][] a=new int[9][10];//标记每一行某个数字
int[][] b=new int[10][9];//标记每一列某个数字
int[][][] c=new int[3][3][10];//标记九宫格中的数字是否重复,这里一共九个九宫格
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(Character.isDigit(board[i][j]))
{
if(a[i][board[i][j]-'0']==1)
return false;
else
a[i][board[i][j]-'0']=1;
if(b[board[i][j]-'0'][j]==1)
return false;
else
b[board[i][j]-'0'][j]=1;
if(c[i/3][j/3][board[i][j]-'0']==1)
return false;
else
c[i/3][j/3][board[i][j]-'0']=1;
}
}
}
return true;
}
}