PS/LeetCode
[Leetcode][Javascript] 36. Valid Sudoku
returnzero
2022. 8. 17. 14:17
1. 1개의 Set사용
/**
* @param {character[][]} board
* @return {boolean}
*/
var isValidSudoku = function(board) {
let set= new Set();
for (let i=0; i<board.length; i++){
for(let j=0; j<board.length; j++){
if(board[i][j]==='.') continue;
else{
let subsquare= (3*Math.floor(i/3) +Math.floor(j/3))
const row=`${i}th row has ${board[i][j]}`
const col=`${j}th column has ${board[i][j]}`
const sub=`${subsquare}th square has ${board[i][j]}`
if(set.has(row)||set.has(col)||set.has(sub)){
return false;
}
else{
set.add(row)
set.add(col)
set.add(sub)
}
}
}
}
return true
};