mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
Time: 127 ms (37.91%), Space: 13.7 MB (7.69%) - LeetHub
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
class Solution(object):
|
||||||
|
def isValidSudoku(self, board):
|
||||||
|
"""
|
||||||
|
:type board: List[List[str]]
|
||||||
|
:rtype: bool
|
||||||
|
"""
|
||||||
|
# Check horizontals
|
||||||
|
for i in range(len(board)):
|
||||||
|
inRow = [False] * 10
|
||||||
|
|
||||||
|
for j in range(len(board[i])):
|
||||||
|
if board[i][j] == ".":
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
if inRow[int(board[i][j])]:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
inRow[int(board[i][j])] = True
|
||||||
|
|
||||||
|
|
||||||
|
# Check verticals
|
||||||
|
for j in range(len(board[0])):
|
||||||
|
inColumn = [False] * 10
|
||||||
|
|
||||||
|
for i in range(len(board)):
|
||||||
|
if board[i][j] == ".":
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
if inColumn[int(board[i][j])]:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
inColumn[int(board[i][j])] = True
|
||||||
|
|
||||||
|
# Check boxes
|
||||||
|
for boxI in range(3):
|
||||||
|
for boxJ in range(3):
|
||||||
|
inBox = [False] * 10
|
||||||
|
for i in range(3):
|
||||||
|
for j in range(3):
|
||||||
|
if board[(boxI * 3) + i][(boxJ * 3) + j] == ".":
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
if inBox[int(board[(boxI * 3) + i][(boxJ * 3) + j])]:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
inBox[int(board[(boxI * 3) + i][(boxJ * 3) + j])] = True
|
||||||
|
|
||||||
|
return True
|
||||||
Reference in New Issue
Block a user