LeetCode - #36 有效的数独

2022-04-04 15:49:52 浏览数 (1)

前言

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

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

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

难度水平:中等

1. 描述

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

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

注意:

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

2. 示例

示例 1

代码语言:javascript复制
输入: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

代码语言:javascript复制
输入: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. 答案

代码语言:javascript复制
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[2]

点击前往 LeetCode[3] 练习

特别感谢 Swift社区 编辑部的每一位编辑,感谢大家的辛苦付出,为 Swift社区 提供优质内容,为 Swift 语言的发展贡献自己的力量,排名不分先后:张安宇@微软[4]戴铭@快手[5]展菲@ESP[6]倪瑶@Trip.com[7]杜鑫瑶@新浪[8]韦弦@Gwell[9]张浩@讯飞[10]张星宇@ByteDance[11]郭英东@便利蜂[12]

参考资料

[1]

@故胤道长: https://m.weibo.cn/u/1827884772

[2]

LeetCode-Swift: https://github.com/soapyigu/LeetCode-Swift

[3]

LeetCode: https://leetcode.com/problems/valid-sudoku/

[4]

张安宇: https://blog.csdn.net/mobanchengshuang

[5]

戴铭: https://ming1016.github.io

[6]

展菲: https://github.com/fanbaoying

[7]

倪瑶: https://github.com/niyaoyao

[8]

杜鑫瑶: https://weibo.com/u/3878455011

[9]

韦弦: https://www.jianshu.com/u/855d6ea2b3d1

[10]

张浩: https://github.com/zhanghao19920218

[11]

张星宇: https://github.com/bestswifter

[12]

郭英东: https://github.com/EmingK

0 人点赞