@@ -191,7 +191,7 @@ class Solution {
191
191
192
192
193
193
194
- 对权重遍历就行优化,提前得到每个空位有可能的值进行遍历,由于对集合进行遍历所以时间不稳定 。最优时间可能在排名在100%
194
+ 对权重遍历就行优化,提前得到每个空位有可能的值进行遍历,由于对集合进行遍历顺序不确定,所以导致时间不稳定 。最优时间可能在排名在100%
195
195
```
196
196
Runtime: 36 ms, faster than 100.00% of Swift online submissions for Sudoku Solver.
197
197
Memory Usage: 22.5 MB, less than 50.00% of Swift online submissions for Sudoku Solver.
@@ -206,12 +206,12 @@ class Solution {
206
206
var clo = [Set < Character > ].init (repeating : Set < Character > (), count : 9 )
207
207
var row = [Set < Character > ].init (repeating : Set < Character > (), count : 9 )
208
208
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 )]()
210
210
for y in 0 ..< 9 {
211
211
for x in 0 ..< 9 {
212
212
let c = board[y][x]
213
213
guard c != " ." else {
214
- points.append ((p : (x : x, y : y), c : Set < Character > () ))
214
+ points.append ((p : (x : x, y : y), c : 0 ))
215
215
continue
216
216
}
217
217
clo[y].insert (c)
@@ -221,11 +221,10 @@ class Solution {
221
221
}
222
222
for i in 0 ..< points.count {
223
223
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
225
225
}
226
- points.sort (by : { $0 .c .count < $1 .c .count })
227
226
_ = fillGrid (index : 0 ,
228
- point : points,
227
+ point : points. sorted ( by : { $0 . c > $1 . c }). map ({ $0 . p }) ,
229
228
board : & board,
230
229
clo : & clo,
231
230
row : & row,
@@ -234,17 +233,16 @@ class Solution {
234
233
235
234
236
235
func fillGrid (index : Int ,
237
- point : [(p: ( x: Int , y: Int ), c: Set < Character > )],
236
+ point : [(x: Int , y: Int )],
238
237
board : inout [[Character ]],
239
238
clo : inout [Set <Character >],
240
239
row : inout [Set <Character >],
241
240
block : inout [Set <Character >]) -> Bool {
242
241
if index == point.count {
243
242
return true
244
243
}
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 ])) {
248
246
board[y][x] = c
249
247
clo[y].insert (c)
250
248
row[x].insert (c)
@@ -261,4 +259,5 @@ class Solution {
261
259
return false
262
260
}
263
261
}
262
+
264
263
```
0 commit comments