728x90
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
1. Each row must contain the digits 1-9 without repetition.
2. Each column must contain the digits 1-9 without repetition.
3. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
Note:
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
Example 1:
nput: board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true
Example 2:
Input: board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Constraints:
- board.length == 9
- board[i].length == 9
- board[i][j] is a digit 1-9 or '.'.
Code :
var isValidSudoku = function(board) {
// 행 검사
for (let row = 0; row < 9; row++) {
const rowSet = new Set();
for (let col = 0; col < 9; col++) {
const cell = board[row][col];
if (cell !== '.' && rowSet.has(cell)) {
return false;
}
if (cell !== '.') {
rowSet.add(cell);
}
}
}
// 열 검사
for (let col = 0; col < 9; col++) {
const colSet = new Set();
for (let row = 0; row < 9; row++) {
const cell = board[row][col];
if (cell !== '.' && colSet.has(cell)) {
return false;
}
if (cell !== '.') {
colSet.add(cell);
}
}
}
// 3x3 서브 그리드 검사
for (let block = 0; block < 9; block++) {
const blockSet = new Set();
const startRow = Math.floor(block / 3) * 3;
const startCol = (block % 3) * 3;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
const cell = board[startRow + i][startCol + j];
if (cell !== '.' && blockSet.has(cell)) {
return false;
}
if (cell !== '.') {
blockSet.add(cell);
}
}
}
}
return true;
};
Solutions Code :
var isValidSudoku = function(board) {
// 각 행, 열, 3x3 박스를 확인하기 위한 반복문
for (let i = 0; i < board.length; i++) {
let rowMap = {};
let colMap = {};
let boxMap = {};
for (let j = 0; j < board[i].length; j++) {
// 현재 위치의 3x3 박스를 찾는 과정
let box = board[3 * Math.floor(i / 3) + Math.floor(j / 3)][3 * (i % 3) + (j % 3)];
// 현재 위치의 값이 '.'이 아닌 경우 유효성을 확인
if (board[i][j] != '.') {
// 현재 행에 값이 중복되는 경우 유효하지 않음
if (rowMap[board[i][j]]) return false;
rowMap[board[i][j]] = 1;
}
if (board[j][i] != '.') {
// 현재 열에 값이 중복되는 경우 유효하지 않음
if (colMap[board[j][i]]) return false;
colMap[board[j][i]] = 1;
}
if (box != '.') {
// 현재 3x3 박스에 값이 중복되는 경우 유효하지 않음
if (boxMap[box]) return false;
boxMap[box] = 1;
}
}
}
return true;
};
출처 : https://leetcode.com/problemset/all/
728x90
'LeetCode' 카테고리의 다른 글
[LeetCode] 38. Count and Say (0) | 2023.11.02 |
---|---|
[LeetCode] 37. Sudoku Solver (0) | 2023.11.02 |
[LeetCode] 35. Search Insert Position (0) | 2023.11.01 |
[LeetCode] 34. Find First and Last Position of Element in Sorted Array (2) | 2023.10.31 |
[LeetCode] 33. Search in Rotated Sorted Array (2) | 2023.10.31 |