I. Description
II. Code
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
# 가로 중복 체크
for row in board:
for i in row:
if i != '.' and row.count(i) > 1:
return False
# 세로 중복 체크
for i in range(9):
col = [row[i] for row in board]
for j in col:
if j != '.' and col.count(j) > 1:
return False
# 3x3 중복 체크
for i in range(0, 9, 3):
for j in range(0, 9, 3):
square = [board[x][y] for x in range(i, i+3) for y in range(j, j+3)]
for k in square:
if k != '.' and square.count(k) > 1:
return False
return True
가로 중복, 세로 중복, 3x3 중복 3가지 경우에 대해 체크해주면 된다. 3x3 중복에서 x, y를 위에 쓰지 않고 안에서 바로 정의하는 게 코드를 간단하게 만들어준다는 것도 기억해놓으면 좋겠다.
'코테 준비 > Hash Table' 카테고리의 다른 글
[LeetCode] 17. Letter Combinations of a Phone Number (0) | 2025.01.25 |
---|---|
[LeetCode] 12. Integer to Roman (0) | 2025.01.25 |
[LeetCode] 1930. Unique Length-3 Palindromic Subsequences (0) | 2025.01.25 |
[LeetCode] 1943. Describe the Painting (0) | 2025.01.23 |
[LeetCode] 1942. The Number of the Smallest Unoccupied Chair (0) | 2025.01.22 |