LeetCode - 36 有效的数独

前言

我们社区陆续会将顾毅(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

1
2
3
4
5
6
7
8
9
10
11
输入: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

1
2
3
4
5
6
7
8
9
10
11
12
输入: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. 答案

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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 有效的数独

文章作者:Swift社区

发布时间:2022年05月25日 - 11:05

最后更新:2022年05月25日 - 11:05

原始链接:https://fanbaoying.github.io/LeetCode-36-有效的数独/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

坚持原创技术分享,您的支持将鼓励我继续创作!