首页 新闻 会员 周边 捐助

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

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

路过的大佬麻烦帮我看一下 为啥条件为False 还是进入if了 晕了

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", ".", ".", ".", ".", ".", ".", ".", "."]]

这个是出错的board值

实在不理解 有没有大佬说一下为啥0.0

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

这个是原算法题

RicardoMYun的主页 RicardoMYun | 菜鸟二级 | 园豆:208
提问于:2022-06-30 10:01
< >
分享
最佳答案
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

改变思路 其实在排除自己本身的时候 只需验证x,y是否等于当前循环到的点的坐标 而不需要考虑其他情况 毕竟 需要排除掉的也就只有这一种情况 就算要验证也应该是8 - k != y而不是8 - k != x 一开始就错了.....

最后就是断点的问题 其实 到目前为止还不理解为啥取到了x=0,y=1的时候 有空再研究一下

 

RicardoMYun | 菜鸟二级 |园豆:208 | 2022-07-21 14:40
其他回答(1)
1

我这里执行的结果是True,单独把函数拿出来结果没问题,是不是调用的时候出错了?

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

其实直接在下方打印返回False时候的值 缩小范围就很好判断了 判断对角线的时候条件判定写错了 不应该是k != x or k - 8 != x......话说要算也应该是8-k.....哪有小的减大的.....这里k-8恒定为非正整数 基本可以认定为恒定为True 所以当board[k][8 - k] == board[x][y]成立 也就是验证到自己本身的时候 直接返回False了 故验证失败

支持(0) 反对(0) RicardoMYun | 园豆:208 (菜鸟二级) | 2022-07-21 14:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册