36. 有效的数独

题目链接:https://leetcode-cn.com/problems/valid-sudoku/submissions/

参考高票题解讲解,很棒。3*3矩阵的序号通过计算存储,恰好能类似运用在73题。
swift版代码

执行用时:80 ms, 在所有 Swift 提交中击败了100.00%的用户
内存消耗:13.9 MB, 在所有 Swift 提交中击败了76.58%的用户

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
class Solution {
func isValidSudoku(_ board: [[Character]]) -> Bool {
var row = [[Int]].init(repeating:
[Int].init(repeating: 0, count: 10), count: 9)
var col = [[Int]].init(repeating:
[Int].init(repeating: 0, count: 10), count: 9)
var block = [[Int]].init(repeating:
[Int].init(repeating: 0, count: 10), count: 9)
for i in 0 ..< 9 {
for j in 0 ..< 9 {
if board[i][j] == "." {
continue
}
let num = Int(String(board[i][j]))!
if row[i][num] == 1 {
return false
}
if col[j][num] == 1 {
return false
}
if block[(i/3)*3 + (j/3)][num] == 1 {
return false
}
row[i][num] = 1
col[j][num] = 1
block[(i/3)*3 + (j/3)][num] = 1
}
}
return true
}
}

36. 有效的数独
https://pisces34.github.io/2021/09/27/leetcode/36/
发布于
2021年9月27日
许可协议