The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.
思路
本题是判断目前九宫给填上的数字是否有效
即:行无重复,列无重复,所在的小九宫格中无重复
所在第几个九宫格: i/3∗3+j/3
class Solution(object):
def isValidSudoku(self,board):
doc={}
doc_rows={}
for i in range(9):
for j in range(9):
if board[i][j]!='.':
if (board[i].count(board[i][j])>1):
return False
doc_rows.setdefault(j,[])
if board[i][j] not in doc_rows[j]:
doc_rows[j].append(board[i][j])
else:
return False
doc.setdefault(i/3*3+j/3,[])
if board[i][j] not in doc[i/3*3+j/3]:
doc[i/3*3+j/3].append(board[i][j])
else:
return False
return True
S=Solution()
board=[[5,3,'.','.',7,'.','.','.','.'],
[6,'.','.',1,9,5,'.','.','.'],
['.',9,8,'.','.','.','.',6,'.'],
[8,'.','.','.',6,'.','.','.',4],
[4,'.','.',8,'.',3,'.','.',1],
[7,'.','.','.',2,'.','.','.',6],
['.',6,'.','.','.','.',2,8,'.'],
['.','.','.',4,1,9,'.','.',5],
['.','.','.','.',8,'.','.',5,9]]
print S.isValidSudoku(board)