【python/leetcode/M】Valid Sudoku
阿新 • • 發佈:2018-11-03
題目
https://leetcode.com/problems/valid-sudoku/description/
基本思路
用三個矩陣分別檢查三個規則是否有重複數字,比如用row, col, block分別檢查行、列、塊是否有重複數字
實現程式碼
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
row = [[False for i in range(9)] for j in range(9)]
col = [[False for i in range(9)] for j in range(9)]
block = [[False for i in range(9)] for j in range(9)]
for i in range(9):
for j in range(9):
if board[i][j] != '.':
num = int(board[i] [j])-1
k = i/3*3+j/3
if row[i][num] or col[j][num] or block[k][num]:
return False
row[i][num] = col[j][num] = block[k][num] = True
return True