36. Valid Sudoku
# Medium
Solution:
Judge repeatition in horizon.
Judge repeatition in vertical.
Judge repeatition in 3x3 blocks.
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
# vertical
for i in range(9):
if self.vertical(board, i) == False:
return False
# horizontal
for i in range(9):
if self.horizon(board, i) == False:
return False
# blocks
for i in range(3):
for j in range(3):
if self.block(board, i, j) == False:
return False
return True
def vertical(self, board, i):
temp = []
for x in range(9):
if board[x][i] != '.':
if board[x][i] in temp:
return False
else:
temp.append(board[x][i])
return True
def horizon(self, board, i):
temp = []
for x in range(9):
if board[i][x] != '.':
if board[i][x] in temp:
return False
else:
temp.append(board[i][x])
return True
def block(self, board, i, j):
#print(sub)
temp = []
for x in range(i*3, (i+1)*3):
for y in range(j*3, (j+1)*3):
if board[x][y] != '.':
if board[x][y] in temp:
return False
else:
temp.append(board[x][y])
return True
list[x]
represents xth
row in Python.
list[:][x]
doesn't represent xth
column in Python.
Last updated
Was this helpful?