[python 刷题] 36 Valid Sudoku

文章讲述了如何使用Python编写一个O(1)复杂度的数独验证函数,通过检查行、列和3x3子区域的唯一性来确定给定的9x9数独板是否有效。给出了三种方法,包括硬编码、使用helperfunction和简洁的字典存储策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[python 刷题] 36 Valid Sudoku

题目:

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.

  2. Each column must contain the digits 1-9 without repetition.

  3. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.

  • Only the filled cells need to be validated according to the mentioned rules.

这道题解数独,其实还是比较简单的,只是验证当前板子上有的数字能否构成一个合法的数独,而一个合法的数独就是每行、每列、每个 2 x 3 的盒子里都只能有不重复的 1-9,按照这个逻辑硬写就行

因为空间大小就是 9 x 9,所以不管怎么跑时间和空间复杂度都是 O ( 1 ) O(1) O(1),非常丑陋的解法:

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        # a valid sodoku needs to have unique value on
        # each row
        # each col
        # each 3x3

        # check each row
        for i in range(9):
            hashset = set()
            for j in range(9):
                if board[i][j] in hashset and board[i][j] != '.':
                    return False
                hashset.add(board[i][j])

        # check each col
        for i in range(9):
            hashset = set()
            for j in range(9):
                if board[j][i] in hashset and board[j][i] != '.':
                    return False
                hashset.add(board[j][i])

        # check 3x3
        for i in range(0, 9, 3):
            for j in range(0, 9, 3):
                hashset = set()
                if board[i][j] in hashset and board[i][j] != '.':
                    return False
                hashset.add(board[i][j])
                if board[i + 1][j] in hashset and board[i + 1][j] != '.':
                    return False
                hashset.add(board[i + 1][j])
                if board[i + 2][j] in hashset and board[i + 2][j] != '.':
                    return False
                hashset.add(board[i + 2][j])

                if board[i][j + 1] in hashset and board[i][j + 1] != '.':
                    return False
                hashset.add(board[i][j + 1])
                if board[i + 1][j + 1] in hashset and board[i + 1][j + 1] != '.':
                    return False
                hashset.add(board[i + 1][j + 1])
                if board[i + 2][j + 1] in hashset and board[i + 2][j + 1] != '.':
                    return False
                hashset.add(board[i + 2][j + 1])

                if board[i][j + 2] in hashset and board[i][j+ 2] != '.':
                    return False
                hashset.add(board[i][j])
                if board[i + 1][j + 2] in hashset and board[i + 1][j + 2] != '.':
                    return False
                hashset.add(board[i + 1][j])
                if board[i + 2][j + 2] in hashset and board[i + 2][j + 2] != '.':
                    return False
                hashset.add(board[i + 2][j + 2])



        return True

其实就是硬解,反正能写完就行

这里提供另外两个稍微好一点的写法,一个是使用几个 helper function:

class Solution:
    def valid_rows(self, board: List[List[str]]) -> bool:
        # return if each row is a valid Sudoku
        return True

    def valid_cols(self, board: List[List[str]]) -> bool:
        # return if each col is a valid Sudoku
        return True

    def valid_three_by_three(self,board: List[List[str]], i: int, j: int) -> bool:
        # return if each 3x3 is a valid Soduku
        for r in range(i, i + 1, 3):
            for c in range(0, i + 1, 3):

        return True

    def valid_nine(self, board: List[List[str]]) -> bool:
        # return if the entire board can be divide into 9 valid 3x3
        for r in range(0, 9, 3):
            for c in range(0, 9, 3):
                if not valid_three_by_three(r, c):
                    return False
        return True

    def isValidSudoku(self, board: List[List[str]]) -> bool:
        return self.valid_rows(board) and self.valid_cols(board) and self.valid_nine(board)

没有写完,因为最后的I写法更好一些

就是用 dict 的写法,这个写法应该是最简洁的了,用 row+col 的组合作为 key,只写一重遍历:

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        cols = collections.defaultdict(set)
        rows = collections.defaultdict(set)
        squares = collections.defaultdict(set)  # key = (r /3, c /3)

        for r in range(9):
            for c in range(9):
                num = board[r][c]
                if num == ".":
                    continue
                if (
                    num in rows[r]
                    or num in cols[c]
                    or num in squares[(r // 3, c // 3)]
                ):
                    return False
                cols[c].add(num)
                rows[r].add(num)
                squares[(r // 3, c // 3)].add(num)

        return True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值