https://leetcode.com/problems/valid-sudoku/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
const checkArr = function(arr) { arr = arr.filter(v => v !== '.') return arr.length === [...new Set(arr)].length }
const isValidSudoku = function(board) { let col = [[], [], [], [], [], [], [], [], []] for(let i = 0; i < 9; i++) { if(!checkArr(board[i])) return false for(let j = 0; j < 9; j++) { col[j].push(board[i][j]) if (i % 3 === 0 && j % 3 === 0) { let box = [] box.push(board[i][j]) box.push(board[i][j + 1]) box.push(board[i][j + 2]) box.push(board[i + 1][j]) box.push(board[i + 1][j + 1]) box.push(board[i + 1][j + 2]) box.push(board[i + 2][j]) box.push(board[i + 2][j + 1]) box.push(board[i + 2][j + 2]) if(!checkArr(box)) return false } } } for(let i = 0; i < col.length; i++) { if(!checkArr(col[i])) return false } return true }
|