- 문제 링크: https://leetcode.com/problems/valid-sudoku/
- 난이도: Medium
걍.. 노가다
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
# rule 1
for row in range(9):
numbers = set()
for col in range(9):
cell = board[row][col]
if cell == ".":
continue
if cell in numbers:
return False
numbers.add(cell)
# rule 2
for col in range(9):
numbers = set()
for row in range(9):
cell = board[row][col]
if cell == ".":
continue
if cell in numbers:
return False
numbers.add(cell)
# rule 3
for sub_row in range(3):
for sub_col in range(3):
numbers = set()
for i in range(9):
row = sub_row*3 + (i // 3)
col = sub_col*3 + (i % 3)
cell = board[row][col]
if cell == ".":
continue
if cell in numbers:
return False
numbers.add(cell)
return True
반응형
'Online Judge > LeetCode' 카테고리의 다른 글
[LeetCode][Python] 125. Valid Palindrome (0) | 2024.09.08 |
---|---|
[LeetCode][Python] 128. Longest Consecutive Sequence (0) | 2024.09.08 |
[LeetCode][Python] 238. Product of Array Except Self (0) | 2024.09.07 |
[LeetCode][Python] 347. Top K Frequent Elements (0) | 2024.09.07 |
[LeetCode][Python] 49. Group Anagrams (0) | 2024.09.07 |