https://leetcode.com/problems/valid-sudoku/?envType=featured-list&envId=top-google-questions
input :
output :
조건 :
Solution explain : Solution1
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
# 행을 기준으로 적절한지 확인
for row in range(len(board)):
temp = {j:0 for j in range(1, 10)}
data = board[row]
for item in data:
if item == ".":
continue
val = int(item)
if temp[val] != 0:
return False
temp[val] += 1
for col in range(len(board)):
# 열을 기준으로 적절한지 확인
temp = {j:0 for j in range(1, 10)}
for row in range(len(board)):
item = board[row][col]
if item == ".":
continue
val = int(item)
if temp[val] != 0:
return False
temp[val] += 1
# 3 * 3으로 분리해서 보았을 때 적절한지 확인.
for x in [0, 3, 6]:
for y in [0, 3, 6]:
used = {j:0 for j in range(1, 10)}
for dx in range(3):
for dy in range(3):
item = board[x + dx][y + dy]
if item == ".":
continue
val = int(item)
if used[val] != 0:
return False
used[val] += 1
return True