# 初级算法题遇到的问题 求大佬帮忙看一下哪里出问题了

0
[已解决问题] 解决于 2022-07-21 14:40

```class Solution(object):
def verify_line(self, board, x, y):
for i in range(9):
if board[i][y] == board[x][y] and i != x:
return False
if board[x][i] == board[x][y] and i != y:
return False
if x == y or x + y == 8:
for k in range(9):
if board[k][k] == board[x][y] and k != x:
return False
if board[k][8 - k] == board[x][y]:
if k != x or k - 8 != x:
return False
return True

def verify_sudoku(self, board, x, y):
for p in range(3):
for q in range(3):
if board[x][y] == board[x + p - x % 3][y + q - y % 3]:
if p - x % 3 != 0 or q - y % 3 != 0:
return False
return True

def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
for u in range(9):
for o in range(9):
if board[u][o] != ".":
if not (self.verify_line(board, u, o) and self.verify_sudoku(board, u, o)):
return False
return True```

old_board = [[".", "8", "7", "6", "5", "4", "3", "2", "1"],
["2", ".", ".", ".", ".", ".", ".", ".", "."],
["3", ".", ".", ".", ".", ".", ".", ".", "."],
["4", ".", ".", ".", ".", ".", ".", ".", "."],
["5", ".", ".", ".", ".", ".", ".", ".", "."],
["6", ".", ".", ".", ".", ".", ".", ".", "."],
["7", ".", ".", ".", ".", ".", ".", ".", "."],
["8", ".", ".", ".", ".", ".", ".", ".", "."],
["9", ".", ".", ".", ".", ".", ".", ".", "."]]

https://leetcode.cn/leetbook/read/top-interview-questions-easy/x2f9gg/

Miss云 | 菜鸟二级 | 园豆：208

0
```class Solution(object):
def verify_line(self, board, x, y):
for i in range(9):
if board[i][y] == board[x][y] and i != x:
return False
if board[x][i] == board[x][y] and i != y:
return False
# 没注意看题目 多此一举 验证了对角线......
# if x == y or x + y == 8:
#     for k in range(9):
#         if board[k][k] == board[x][y] and k != x:
#             return False
#         if board[k][8 - k] == board[x][y] and k != x:
#             return False
return True

def verify_sudoku(self, board, x, y):
for p in range(3):
for q in range(3):
if board[x][y] == board[x + p - x % 3][y + q - y % 3]:
if p - x % 3 == 0 and q - y % 3 == 0:
continue
return False
# if p - x % 3 != 0 or q - y % 3 != 0:
#     return False
return True

def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
for u in range(9):
for o in range(9):
if board[u][o] != ".":
if not (self.verify_line(board, u, o) and self.verify_sudoku(board, u, o)):
return False
return True```

Miss云 | 菜鸟二级 |园豆：208 | 2022-07-21 14:40

1

Sunny-King | 园豆：208 (菜鸟二级) | 2022-07-14 15:15

您需要登录以后才能回答，未注册用户请先注册