题目:
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules .
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
Note:A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
分析:题目其实没有难度,按照它的要求做就行了。代码已通过检测
1 def isValidSudoku(self, board): 2 row = 0 3 column = 0 4 flag = 0 5 6 #查看每一行是否符合规则 7 while row < 9: 8 i = 0 9 temp = [] 10 while i < 9: 11 if board[row][i] != '.': 12 if board[row][i] not in temp: 13 temp.append(board[row][i]) 14 i += 1 15 else: 16 flag = 1 17 break 18 else: 19 i += 1 20 21 if flag == 1: 22 break 23 else: 24 row += 1 25 26 if flag == 1: 27 return False 28 else: 29 #查看每一行是否符合规则 30 while column < 9: 31 j = 0 32 temp = [] 33 while j < 9: 34 if board[j][column] != '.': 35 if board[j][column] not in temp: 36 temp.append(board[j][column]) 37 j += 1 38 else: 39 flag = 1 40 break 41 else: 42 j += 1 43 44 if flag == 1: 45 break 46 else: 47 column += 1 48 49 if flag == 1: 50 return False 51 else: 52 #查看每个九宫格是否符合规则 53 row = 0 54 column = 0 55 while row < 7: 56 while column < 7: 57 r = 0 58 c = 0 59 temp = [] 60 while r < 3: 61 while c < 3: 62 if board[row+r][column+c] != '.': 63 if board[row+r][column+c] not in temp: 64 temp.append(board[row+r][column+c]) 65 c += 1 66 else: 67 flag = 1 68 break 69 else: 70 c += 1 71 if flag == 1: 72 break 73 else: 74 r += 1 75 c = 0 76 77 if flag == 1: 78 break 79 else: 80 column += 3 81 82 if flag == 1: 83 break 84 else: 85 row += 3 86 column = 0 87 88 if flag == 1: 89 return False 90 else: 91 return True