Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit 2b22144

Browse files
authored
Update 37. Sudoku Solver.md
1 parent 7cf4d1e commit 2b22144

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

‎37. Sudoku Solver.md‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ A sudoku puzzle...
2727

2828
# Solution
2929

30-
扫描遍历
30+
顺序遍历
3131
```swift
3232

3333
class Solution {
@@ -101,13 +101,16 @@ class Solution {
101101
```swift
102102

103103
class Solution {
104+
104105
static let num: [Character] = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
106+
105107
func solveSudoku(_ board: inout [[Character]]) {
106108
var clo = [Set<Character>].init(repeating: Set<Character>(), count: 9)
107109
var row = [Set<Character>].init(repeating: Set<Character>(), count: 9)
108110
var block = [Set<Character>].init(repeating: Set<Character>(), count: 9)
109111
var points = [(p: (x: Int, y: Int), c: Int)]()
110112
for y in 0..<9 {
113+
for x in 0..<9 {
111114
let c = board[y][x]
112115
guard c != "." else {
113116
points.append((p: (x: x, y: y), c: 0))
@@ -118,12 +121,16 @@ class Solution {
118121
block[x/3 + (y/3) * 3].insert(c)
119122
}
120123
}
121-
let _clo = clo.map({ 0ドル.count })
122-
let _row = row.map({ 0ドル.count })
123-
let _block = block.map({ 0ドル.count })
124124
for i in 0..<points.count {
125125
let (x, y) = points[i].p
126-
points[i].c = _clo[y] + _row[x] + _block[x/3 + (y/3) * 3]
126+
var set = clo[y]
127+
for c in row[x] {
128+
set.insert(c)
129+
}
130+
for c in block[x/3 + (y/3) * 3] {
131+
set.insert(c)
132+
}
133+
points[i].c = set.count
127134
}
128135
_ = fillGrid(index: 0,
129136
point: points.sorted(by: { 0ドル.c > 1ドル.c }).map({ 0ドル.p }),
@@ -164,6 +171,7 @@ class Solution {
164171
}
165172
}
166173

174+
167175
```
168176

169177
```
@@ -178,7 +186,7 @@ class Solution {
178186
[".",".",".","4","1","9",".",".","5"],
179187
[".",".",".",".","8",".",".","7","9"]]
180188
181-
扫描遍历调用fillGrid 4208次,按权重遍历调用fillGrid 340次
189+
顺序遍历调用fillGrid 4209次,按权重遍历调用fillGrid 103次
182190
183191
```
184192

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /