Skip to content

Commit

Permalink
Update 0036-valid-sudoku.py
Browse files Browse the repository at this point in the history
Reduces unnecessary list indexing, making the code faster. Beats 99% on time on leetcode. consider merging
  • Loading branch information
brayo-pip authored Sep 11, 2023
1 parent 2ae20b0 commit 2a39622
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions python/0036-valid-sudoku.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ def isValidSudoku(self, board: List[List[str]]) -> bool:

for r in range(9):
for c in range(9):
if board[r][c] == ".":
num = board[r][c]
if num == ".":
continue
if (
board[r][c] in rows[r]
or board[r][c] in cols[c]
or board[r][c] in squares[(r // 3, c // 3)]
num in rows[r]
or num in cols[c]
or num in squares[(r // 3, c // 3)]
):
return False
cols[c].add(board[r][c])
rows[r].add(board[r][c])
squares[(r // 3, c // 3)].add(board[r][c])
cols[c].add(num)
rows[r].add(num)
squares[(r // 3, c // 3)].add(num)

return True

0 comments on commit 2a39622

Please sign in to comment.