@@ -27,7 +27,7 @@ A sudoku puzzle...
27
27
28
28
# Solution
29
29
30
- 扫描遍历
30
+ 顺序遍历
31
31
``` swift
32
32
33
33
class Solution {
@@ -101,13 +101,16 @@ class Solution {
101
101
``` swift
102
102
103
103
class Solution {
104
+
104
105
static let num: [Character ] = [" 1" , " 2" , " 3" , " 4" , " 5" , " 6" , " 7" , " 8" , " 9" ]
106
+
105
107
func solveSudoku (_ board : inout [[Character ]]) {
106
108
var clo = [Set < Character > ].init (repeating : Set < Character > (), count : 9 )
107
109
var row = [Set < Character > ].init (repeating : Set < Character > (), count : 9 )
108
110
var block = [Set < Character > ].init (repeating : Set < Character > (), count : 9 )
109
111
var points = [(p : (x : Int , y : Int ), c : Int )]()
110
112
for y in 0 ..< 9 {
113
+ for x in 0 ..< 9 {
111
114
let c = board[y][x]
112
115
guard c != " ." else {
113
116
points.append ((p : (x : x, y : y), c : 0 ))
@@ -118,12 +121,16 @@ class Solution {
118
121
block[x/ 3 + (y/ 3 ) * 3 ].insert (c)
119
122
}
120
123
}
121
- let _clo = clo.map ({ $0 .count })
122
- let _row = row.map ({ $0 .count })
123
- let _block = block.map ({ $0 .count })
124
124
for i in 0 ..< points.count {
125
125
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
127
134
}
128
135
_ = fillGrid (index : 0 ,
129
136
point : points.sorted (by : { $0 .c > $1 .c }).map ({ $0 .p }),
@@ -164,6 +171,7 @@ class Solution {
164
171
}
165
172
}
166
173
174
+
167
175
```
168
176
169
177
```
@@ -178,7 +186,7 @@ class Solution {
178
186
[".",".",".","4","1","9",".",".","5"],
179
187
[".",".",".",".","8",".",".","7","9"]]
180
188
181
- 扫描遍历调用fillGrid 4208次 ,按权重遍历调用fillGrid 340次
189
+ 顺序遍历调用fillGrid 4209次 ,按权重遍历调用fillGrid 103次
182
190
183
191
```
184
192
0 commit comments