讨论社区

36 题 测试结果与本地编辑器不同 Pycharm输出的是false 提交时提示错误 输出为true

class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        # board 是一个列表 他里面有9个元素
        # 每个元素又都是一个列表 且这个列表里面也有9个元素都是字符型的
        # 首先判断每一行 即board 中每一个列表是否有重复元素,得把非空数据拿出来
        # 然后判断每一列上 是否有重复的
        # 最后判断每3×3

        # 判断行
        for res in board:
            l = []
            for i in range(len(res)):
                if res[i] != ".":
                    l.append(int(res[i]))
            a = sorted(l)
            l = sorted(set(l))
            # print(a)
            # print(l)
            if l != a:
                return "false"
        # return "true"

        # 判断每一列
        for i in range(len(board)):
            a = []
            for res in board:
                a.append(res[i])
            # a是每一列第i个元素
            l = []
            for j in a:
                if j != ".":
                    l.append(int(j))
            a = sorted(l)
            l = sorted(set(l))
            if l != a:
                return "false"
        # return "true"

        # 判断每 3×3
        for i in range(0, len(board), 3):
            # 一次取出board中的三个数据
            res =board[i:i+3]
            # 构造3×3
            for i in range(0, len(res), 3):
                a = []
                for ress in res:
                    a.append(ress[i])
                    a.append(ress[i + 1])
                    a.append(ress[i + 2])
                l = []
                for j in a:
                    if j != ".":
                        l.append(int(j))
                a = sorted(l)
                l = sorted(set(l))
                if l != a:
                    return "false"
        return "true"


if __name__ == "__main__":
    a = Solution()
    print(a.isValidSudoku([["8","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]))

0 人关注了该问题 关注

0

刘岳 • 2月前

测试结果没问题,我这边显示的是False,你的返回值不要用字符串,要用布尔类型。


0 个讨论

您需要登录后才可回复
您需要登录后才可以回复