思路
var isValidSudoku = function(board) {
const rows = new Array(9).fill(0).map(() => new Array(9).fill(0));
const columns = new Array(9).fill(0).map(() => new Array(9).fill(0));
const boxes = new Array(9).fill(0).map(() => new Array(9).fill(0));
for(let i = 0; i < 9; i++){
for(let j = 0; j < 9; j++){
if(board[i][j] == '.')continue;
const tmp = parseInt(board[i][j]);
if(rows[i][tmp] == 1)return false;
if(columns[j][tmp] == 1)return false;
if(boxes[parseInt(i / 3) * 3 + parseInt(j / 3)][tmp] == 1)return false;
rows[i][tmp] = 1;
columns[j][tmp] = 1;
boxes[parseInt(i / 3) * 3 + parseInt(j / 3)][tmp] = 1;
}
}
return true;
}
|