File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
lcof2/剑指 Offer II 109. 开密码锁 Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -236,6 +236,47 @@ public:
236
236
};
237
237
```
238
238
239
+ #### Go
240
+
241
+ ```go
242
+ func openLock(deadends []string, target string) int {
243
+ dead := map[string]bool{}
244
+ for _, s := range deadends {
245
+ dead[s] = true
246
+ }
247
+ if dead["0000"] {
248
+ return -1
249
+ }
250
+ if target == "0000" {
251
+ return 0
252
+ }
253
+ q := []string{"0000"}
254
+ visited := map[string]bool{"0000": true}
255
+ step := 0
256
+ for len(q) > 0 {
257
+ step++
258
+ size := len(q)
259
+ for i := 0; i < size; i++ {
260
+ cur := q[0]
261
+ q = q[1:]
262
+ for j := 0; j < 4; j++ {
263
+ for k := -1; k <= 1; k += 2 {
264
+ next := cur[:j] + string((cur[j]-'0'+byte(k)+10)%10+'0') + cur[j+1:]
265
+ if next == target {
266
+ return step
267
+ }
268
+ if !dead[next] && !visited[next] {
269
+ q = append(q, next)
270
+ visited[next] = true
271
+ }
272
+ }
273
+ }
274
+ }
275
+ }
276
+ return -1
277
+ }
278
+ ```
279
+
239
280
<!-- tabs:end -->
240
281
241
282
<!-- solution:end -->
You can’t perform that action at this time.
0 commit comments