Skip to content

Commit da218b8

Browse files
committed
feat: add solutions to lc problem: No.0752
No.0752.Open the Lock
1 parent cca41c2 commit da218b8

File tree

6 files changed

+348
-247
lines changed

6 files changed

+348
-247
lines changed

solution/0700-0799/0752.Open the Lock/README.md

Lines changed: 118 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69+
BFS 最小步数模型。
70+
6971
<!-- tabs:start -->
7072

7173
### **Python3**
@@ -75,41 +77,36 @@
7577
```python
7678
class Solution:
7779
def openLock(self, deadends: List[str], target: str) -> int:
78-
s = set(deadends)
79-
if target in s or '0000' in s:
80-
return -1
81-
if target == '0000':
82-
return 0
83-
84-
def prev(c):
85-
return '9' if c == '0' else str(int(c) - 1)
86-
87-
def next(c):
88-
return '0' if c == '9' else str(int(c) + 1)
89-
90-
def get(t):
80+
def next(s):
9181
res = []
92-
t = list(t)
82+
s = list(s)
9383
for i in range(4):
94-
c = t[i]
95-
t[i] = prev(c)
96-
res.append(''.join(t))
97-
t[i] = next(c)
98-
res.append(''.join(t))
99-
t[i] = c
84+
c = s[i]
85+
s[i] = '9' if c == '0' else str(int(c) - 1)
86+
res.append(''.join(s))
87+
s[i] = '0' if c == '9' else str(int(c) + 1)
88+
res.append(''.join(s))
89+
s[i] = c
10090
return res
10191

102-
visited = set()
103-
q = deque([('0000', 0)])
92+
if target == '0000':
93+
return 0
94+
s = set(deadends)
95+
if '0000' in s:
96+
return -1
97+
q = deque([('0000')])
98+
s.add('0000')
99+
ans = 0
104100
while q:
105-
status, step = q.popleft()
106-
for t in get(status):
107-
if t in visited or t in s:
108-
continue
109-
if t == target:
110-
return step + 1
111-
q.append((t, step + 1))
112-
visited.add(t)
101+
ans += 1
102+
for _ in range(len(q), 0, -1):
103+
p = q.popleft()
104+
for t in next(p):
105+
if t == target:
106+
return ans
107+
if t not in s:
108+
q.append(t)
109+
s.add(t)
113110
return -1
114111
```
115112

@@ -120,52 +117,43 @@ class Solution:
120117
```java
121118
class Solution {
122119
public int openLock(String[] deadends, String target) {
120+
if ("0000".equals(target)) {
121+
return 0;
122+
}
123123
Set<String> s = new HashSet<>(Arrays.asList(deadends));
124-
if (s.contains(target) || s.contains("0000")) {
124+
if (s.contains("0000")) {
125125
return -1;
126126
}
127-
if (Objects.equals(target, "0000")) {
128-
return 0;
129-
}
130-
Set<String> visited = new HashSet<>();
131127
Deque<String> q = new ArrayDeque<>();
132-
q.offerLast("0000");
133-
int step = 0;
128+
q.offer("0000");
129+
s.add("0000");
130+
int ans = 0;
134131
while (!q.isEmpty()) {
135-
++step;
136-
for (int i = 0, n = q.size(); i < n; ++i) {
137-
String status = q.pollFirst();
138-
for (String t : get(status)) {
139-
if (visited.contains(t) || s.contains(t)) {
140-
continue;
132+
++ans;
133+
for (int n = q.size(); n > 0; --n) {
134+
String p = q.poll();
135+
for (String t : next(p)) {
136+
if (target.equals(t)) {
137+
return ans;
141138
}
142-
if (Objects.equals(t, target)) {
143-
return step;
139+
if (!s.contains(t)) {
140+
q.offer(t);
141+
s.add(t);
144142
}
145-
q.offerLast(t);
146-
visited.add(t);
147143
}
148144
}
149145
}
150146
return -1;
151147
}
152148

153-
private char prev(char c) {
154-
return c == '0' ? '9' : (char) (c - 1);
155-
}
156-
157-
private char next(char c) {
158-
return c == '9' ? '0' : (char) (c + 1);
159-
}
160-
161-
private List<String> get(String t) {
149+
private List<String> next(String t) {
162150
List res = new ArrayList<>();
163151
char[] chars = t.toCharArray();
164152
for (int i = 0; i < 4; ++i) {
165153
char c = chars[i];
166-
chars[i] = prev(c);
154+
chars[i] = c == '0' ? '9' : (char) (c - 1);
167155
res.add(String.valueOf(chars));
168-
chars[i] = next(c);
156+
chars[i] = c == '9' ? '0' : (char) (c + 1);
169157
res.add(String.valueOf(chars));
170158
chars[i] = c;
171159
}
@@ -181,47 +169,40 @@ class Solution {
181169
public:
182170
int openLock(vector<string>& deadends, string target) {
183171
unordered_set<string> s(deadends.begin(), deadends.end());
184-
if (s.count(target) || s.count("0000")) return -1;
172+
if (s.count("0000")) return -1;
185173
if (target == "0000") return 0;
186-
unordered_set<string> visited;
187-
queue<string> q;
188-
q.push("0000");
189-
int step = 0;
174+
queue<string> q{{"0000"}};
175+
s.insert("0000");
176+
int ans = 0;
190177
while (!q.empty())
191178
{
192-
++step;
193-
for (int i = 0, n = q.size(); i < n; ++i)
179+
++ans;
180+
for (int n = q.size(); n > 0; --n)
194181
{
195-
string status = q.front();
182+
string p = q.front();
196183
q.pop();
197-
for (auto t : get(status))
184+
for (string t : next(p))
198185
{
199-
if (visited.count(t) || s.count(t)) continue;
200-
if (t == target) return step;
201-
q.push(t);
202-
visited.insert(t);
186+
if (target == t) return ans;
187+
if (!s.count(t))
188+
{
189+
q.push(t);
190+
s.insert(t);
191+
}
203192
}
204193
}
205194
}
206195
return -1;
207196
}
208197

209-
char prev(char c) {
210-
return c == '0' ? '9' : (char) (c - 1);
211-
}
212-
213-
char next(char c) {
214-
return c == '9' ? '0' : (char) (c + 1);
215-
}
216-
217-
vector<string> get(string& t) {
198+
vector<string> next(string& t) {
218199
vector<string> res;
219200
for (int i = 0; i < 4; ++i)
220201
{
221202
char c = t[i];
222-
t[i] = prev(c);
203+
t[i] = c == '0' ? '9' : (char) (c - 1);
223204
res.push_back(t);
224-
t[i] = next(c);
205+
t[i] = c == '9' ? '0' : (char) (c + 1);
225206
res.push_back(t);
226207
t[i] = c;
227208
}
@@ -230,6 +211,61 @@ public:
230211
};
231212
```
232213

214+
### **Go**
215+
216+
```go
217+
func openLock(deadends []string, target string) int {
218+
if target == "0000" {
219+
return 0
220+
}
221+
s := make(map[string]bool)
222+
for _, d := range deadends {
223+
s[d] = true
224+
}
225+
if s["0000"] {
226+
return -1
227+
}
228+
q := []string{"0000"}
229+
s["0000"] = true
230+
ans := 0
231+
next := func(t string) []string {
232+
s := []byte(t)
233+
var res []string
234+
for i, b := range s {
235+
s[i] = b - 1
236+
if s[i] < '0' {
237+
s[i] = '9'
238+
}
239+
res = append(res, string(s))
240+
s[i] = b + 1
241+
if s[i] > '9' {
242+
s[i] = '0'
243+
}
244+
res = append(res, string(s))
245+
s[i] = b
246+
}
247+
return res
248+
}
249+
for len(q) > 0 {
250+
ans++
251+
for n := len(q); n > 0; n-- {
252+
p := q[0]
253+
q = q[1:]
254+
for _, t := range next(p) {
255+
if target == t {
256+
return ans
257+
}
258+
if !s[t] {
259+
q = append(q, t)
260+
s[t] = true
261+
}
262+
}
263+
}
264+
}
265+
return -1
266+
}
267+
```
268+
233269
### **...**
234270

235271
```

0 commit comments

Comments
 (0)