Skip to content
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit 15446b2

Browse files
authored
Update 37. Sudoku Solver.md
1 parent a35aef5 commit 15446b2

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

37. Sudoku Solver.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class Solution {
191191

192192

193193

194-
对权重遍历就行优化,提前得到每个空位有可能的值进行遍历,由于对集合进行遍历所以时间不稳定。最优时间可能在排名在100%
194+
对权重遍历就行优化,提前得到每个空位有可能的值进行遍历,由于对集合进行遍历顺序不确定,所以导致时间不稳定。最优时间可能在排名在100%
195195
```
196196
Runtime: 36 ms, faster than 100.00% of Swift online submissions for Sudoku Solver.
197197
Memory Usage: 22.5 MB, less than 50.00% of Swift online submissions for Sudoku Solver.
@@ -206,12 +206,12 @@ class Solution {
206206
var clo = [Set<Character>].init(repeating: Set<Character>(), count: 9)
207207
var row = [Set<Character>].init(repeating: Set<Character>(), count: 9)
208208
var block = [Set<Character>].init(repeating: Set<Character>(), count: 9)
209-
var points = [(p: (x: Int, y: Int), c: Set<Character>)]()
209+
var points = [(p: (x: Int, y: Int), c: Int)]()
210210
for y in 0..<9 {
211211
for x in 0..<9 {
212212
let c = board[y][x]
213213
guard c != "." else {
214-
points.append((p: (x: x, y: y), c: Set<Character>()))
214+
points.append((p: (x: x, y: y), c: 0))
215215
continue
216216
}
217217
clo[y].insert(c)
@@ -221,11 +221,10 @@ class Solution {
221221
}
222222
for i in 0..<points.count {
223223
let (x, y) = points[i].p
224-
points[i].c = Solution.num.subtracting(clo[y].union(row[x]).union(block[x/3 + (y/3) * 3]))
224+
points[i].c = clo[y].union(row[x]).union(block[x/3 + (y/3) * 3]).count
225225
}
226-
points.sort(by: { $0.c.count < $1.c.count })
227226
_ = fillGrid(index: 0,
228-
point: points,
227+
point: points.sorted(by: { $0.c > $1.c }).map({ $0.p }),
229228
board: &board,
230229
clo: &clo,
231230
row: &row,
@@ -234,17 +233,16 @@ class Solution {
234233

235234

236235
func fillGrid(index: Int,
237-
point: [(p: (x: Int, y: Int), c: Set<Character>)],
236+
point: [(x: Int, y: Int)],
238237
board: inout [[Character]],
239238
clo: inout [Set<Character>],
240239
row: inout [Set<Character>],
241240
block: inout [Set<Character>]) -> Bool {
242241
if index == point.count {
243242
return true
244243
}
245-
let (x, y) = point[index].p
246-
247-
for c in point[index].c.subtracting(clo[y].union(row[x]).union(block[x/3 + (y/3) * 3])) {
244+
let (x, y) = point[index]
245+
for c in Solution.num.subtracting(clo[y].union(row[x]).union(block[x/3 + (y/3) * 3])) {
248246
board[y][x] = c
249247
clo[y].insert(c)
250248
row[x].insert(c)
@@ -261,4 +259,5 @@ class Solution {
261259
return false
262260
}
263261
}
262+
264263
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy