比较了上下左右各9行以及9个小矩形,应该有简单点的方法吧。算法如下:
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
import re
def decide(x): #x为list
a = ''.join(x)
b = re.sub('\.', '', a)
len1 = len(b)
len2 = len(set(b))
if len1 != len2:
return False
else:
return True
for i in board:
a = list(i)
decide1 = decide(a)
if decide1 == False:
return False
list1 = []
for i in range(9):
for j in range(9):
list1.append(board[j][i])
decide2 = decide(list1)
if decide2 == False:
return False
list1 = []
for i in range(0, 9, 3):
for j in range(0, 9, 3):
list1 = list1 + list(board[i][j:j + 3]) + list(board[i + 1][j:j + 3]) + list(board[i + 2][j:j + 3])
decide3 = decide(list1)
if decide3 == False:
return False
list1 = []
return True