• 【LeetCode】36. 有效的数独


    1 问题

    请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。

    数字 1-9 在每一行只能出现一次。
    数字 1-9 在每一列只能出现一次。
    数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)

    注意:

    一个有效的数独(部分已被填充)不一定是可解的。
    只需要根据以上规则,验证已经填入的数字是否有效即可。
    空白格用 ‘.’ 表示。

    示例 1:

    在这里插入图片描述
    输入:board =

    [["5","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"]]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输出:true
    示例 2:

    输入:board =

    [["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"]]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    输出:false
    解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

    2 答案

    这题直接不会
    暴力法,自己写的,速度比较快

    class Solution:
        def isValidSudoku(self, board: List[List[str]]) -> bool:
            
            for i in range(len(board)):
                hashmap = {}
                for j in range(len(board[0])):
                    if board[i][j] != '.':
                        hashmap[board[i][j]] = hashmap.get(board[i][j], 0) + 1
                        if hashmap[board[i][j]] > 1:
                            return False
            
            for j in range(len(board[0])):
                hashmap = {}
                for i in range(len(board)):
                    if board[i][j] != '.':
                        hashmap[board[i][j]] = hashmap.get(board[i][j], 0) + 1
                        if hashmap[board[i][j]] > 1:
                            return False
            
            
            for i in range(3):
                for j in range(3):
                    hashmap = {} 
                    for m in range(len(board)):
                        for n in range(len(board[0])):
                            if board[m][n] != '.' and m // 3 == i and n // 3 == j:
                                hashmap[board[m][n]] = hashmap.get(board[m][n], 0) + 1
                                if hashmap[board[m][n]] > 1:
                                    return False  
            return True
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述

    官方解,利用哈希表,哈希表也可以是列表,矩阵的表示方法也很妙,非常值得学习

    class Solution:
        def isValidSudoku(self, board: List[List[str]]) -> bool:
    
            row = [[0] * 9 for _ in range(9)]
            col = [[0] * 9 for _ in range(9)]
            block = [[0] * 9 for _ in range(9)]
    
            for i in range(9):
                for j in range(9):
                    if board[i][j] != '.':
                        num = int(board[i][j]) - 1
                        b = (i // 3) * 3 + j // 3   # 3*3 小矩阵的第b个矩阵,这个很妙
                        if row[i][num] or col[j][num] or block[b][num]:  # 看这几个位置是否有值,如果有一个有,则返回 False
                            return False
                        row[i][num] = col[j][num] = block[b][num] = 1
            return True
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3 知识点

    哈希表(Hash Table):也叫做散列表。是根据关键码值(Key Value)直接进行访问的数据结构。

    哈希表通过「键 key 」和「映射函数 Hash(key) 」计算出对应的「值value」,把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做「哈希函数(散列函数)」,存放记录的数组叫做「哈希表(散列表)」。

    https://blog.csdn.net/zy_dreamer/article/details/131036258

  • 相关阅读:
    【LeetCode:2736. 最大和查询 | 贪心 + 二分 + 单调栈】
    nginx-带宽限制-令牌桶算法
    LeetCode 热题 HOT 100(更新中)
    【ElasticSearch系列-05】SpringBoot整合elasticSearch
    Docker Compose快速入门及实战
    什么是CPU密集型、IO密集型?什么是多进程与线程和协程?并行与并发?
    Lua学习笔记:require非.lua拓展名的文件
    多项式算法——快速数论变换NTT
    Java面试题大全(整理版)1000+面试题附答案详解,最全面详细,看完稳了
    Java BufferedWriter.write()具有什么功能呢?
  • 原文地址:https://blog.csdn.net/CSDNLHCC/article/details/133895900