문제: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits 1-9 without repetition.
- Each column must contain the digits 1-9 without repetition.
- Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
Example 1:
Input: [
["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: [
["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.
-문제해결 정리-
1. 3개의 constraints들을 check 해줄 function 3개를 따로 만들어준다. (ValidRow, ValidCol, ValidBox)
2. ValidRow - 각 row들의 element 들을 돌며, 같은 숫자가 있는지 체크 (같은 숫자가 있다면 False return)
ValidCol - 각 column 의 element 들을 돌며, 같은 숫자가 있는지 체크 (같은 숫자가 있다면 False return)
ValidBox - 각 3X3의 박스안의 element 들을 돌며, 같은 숫자가 있는지 체크 (같은 숫자가 있다면 False return)
3. 모든 helper function 들을 돌고난후에 return 값이 false 가 없으면 valid 한 sudoku 이므로 True값 return.
모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]
'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글
LeetCode 48. Rotate Image (0) | 2020.05.22 |
---|---|
LeetCode 283. Move Zeroes (0) | 2020.05.22 |
LeetCode 189. Rotate Array (0) | 2020.05.22 |
LeetCode 01. Two Sum (0) | 2020.05.21 |
LeetCode 344. Reverse String [Easy] (0) | 2020.05.20 |
댓글