注释1:每3行为一组 i/3表示第几组 又*3表示每块是3*3 每3列为一组 j/3表示是第几组
将数据块分为
0 1 2
3 4 5
6 7 8 块
blockIndex表示第几块
public boolean isValidSudoku(char[][] board) {
//存储每一行 出现的数字
boolean [][] r = new boolean[9][9];
//存储每一列 出现的数字
boolean [][] c = new boolean[9][9];
//存储每一3*3块 出现的数字
boolean [][] bock = new boolean[9][9];
for(int i = 0;i < 9 ; i++){
for(int j = 0; j < 9 ;j ++){
if(board[i][j]!='.'){
int num = board[i][j] - '1';//将1-9数据将为0-8
int blockIndex = i / 3 * 3 + j / 3;//注释1
if(r[i][num] || c[j][num] || bock[blockIndex][num]){
return false;
}else{
r[i][num] = true;
c[j][num] = true;
bock[blockIndex][num] = true;
}
}
}
}
return true;
}