美文网首页LeetCodeSwift 集
LeetCode - #36 有效的数独

LeetCode - #36 有效的数独

作者: Swift社区 | 来源:发表于2022-04-07 11:33 被阅读0次

    前言

    我们社区陆续会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。微博:@故胤道长)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。

    LeetCode 算法到目前我们已经更新了 35 期,我们会保持更新时间和进度(周一、周三、周五早上 9:00 发布),每期的内容不多,我们希望大家可以在上班路上阅读,长久积累会有很大提升。

    不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。如果大家有建议和意见欢迎在文末留言,我们会尽力满足大家的需求。

    难度水平:中等

    1. 描述

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

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

    注意:

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

    2. 示例

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

    约束条件:

    • board.length == 9
    • board[i].length == 9
    • board[i][j]`` 是一位数字(1-9)或者'.'`

    3. 答案

    class ValidSudoku {
        func isValidSudoku(_ board: [[Character]]) -> Bool {
            return areRowsValid(board) && areColsValid(board) && areSubsquaresValid(board)
        }
        
        private func areRowsValid(_ board: [[Character]]) -> Bool {
            var existingDigits = Set<Character>()
            
            for i in 0..<board.count {
                existingDigits.removeAll()
                
                for j in 0..<board[0].count {
                    if !isDigitValid(board[i][j], &existingDigits) {
                        return false
                    }
                }
            }
            
            return true
        }
        
        private func areColsValid(_ board: [[Character]]) -> Bool {
            var existingDigits = Set<Character>()
            
            for i in 0..<board[0].count {
                existingDigits.removeAll()
                
                for j in 0..<board.count {
                    if !isDigitValid(board[j][i], &existingDigits) {
                        return false
                    }
                }
            }
            
            return true
        }
        
        private func areSubsquaresValid(_ board: [[Character]]) -> Bool {
            var existingDigits = Set<Character>()
            
            for i in stride(from: 0, to: board.count, by: 3) {
                for j in stride(from: 0, to: board[0].count, by: 3) {
                    existingDigits.removeAll()
                    
                    for m in i..<i + 3 {
                        for n in j..<j + 3 {
                            if !isDigitValid(board[m][n], &existingDigits) {
                                return false
                            }
                        }
                    }
                }
            }
            
            return true
        }
        
        private func isDigitValid(_ digit: Character, _ set: inout Set<Character>) -> Bool {
            if digit == "." {
                return true
            }
            
            if set.contains(digit) {
                return false
            } else {
                set.insert(digit)
                return true
            }
        }
    }
    
    • 主要思想:分别检查行、列和单个正方形。
    • 时间复杂度: O(n^2)
    • 空间复杂度: O(1)

    该算法题解的仓库:LeetCode-Swift

    点击前往 LeetCode 练习

    关于我们

    我们是由 Swift 爱好者共同维护,我们会分享以 Swift 实战、SwiftUI、Swift 基础为核心的技术内容,也整理收集优秀的学习资料。

    后续还会翻译大量资料到我们公众号,有感兴趣的朋友,可以加入我们。

    相关文章

      网友评论

        本文标题:LeetCode - #36 有效的数独

        本文链接:https://www.haomeiwen.com/subject/bzxadrtx.html