@@ -27,7 +27,7 @@ A sudoku puzzle...
2727
2828# Solution
2929
30- 扫描遍历
30+ 顺序遍历
3131``` swift
3232
3333class Solution {
@@ -101,13 +101,16 @@ class Solution {
101101``` swift
102102
103103class 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