Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6521de1

Browse files
✨update: Modify 752
1 parent f1f673d commit 6521de1

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

‎LeetCode/751-760/752. 打开转盘锁(中等).md‎

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ class Solution {
146146
Deque<String> d1 = new ArrayDeque<>(), d2 = new ArrayDeque<>();
147147
/*
148148
* m1 和 m2 分别记录两个方向出现的状态是经过多少次转换而来
149-
* e.g
150-
* m1 = {"1000":1} 代表 "1000" 由 s="0000" 替换 1 次字符而来
151-
* m2 = {"9999":3} 代表 "9999" 由 t="9996" 替换 3 次字符而来
149+
* e.g.
150+
* m1 = {"1000":1} 代表 "1000" 由 s="0000" 旋转 1 次而来
151+
* m2 = {"9999":3} 代表 "9999" 由 t="9996" 旋转 3 次而来
152152
*/
153153
Map<String, Integer> m1 = new HashMap<>(), m2 = new HashMap<>();
154154
d1.addLast(s);
@@ -174,33 +174,36 @@ class Solution {
174174
return -1;
175175
}
176176
int update(Deque<String> deque, Map<String, Integer> cur, Map<String, Integer> other) {
177-
String poll = deque.pollFirst();
178-
char[] pcs = poll.toCharArray();
179-
int step = cur.get(poll);
180-
// 枚举替换哪个字符
181-
for (int i = 0; i < 4; i++) {
182-
// 能「正向转」也能「反向转」,这里直接枚举偏移量 [-1,1] 然后跳过 0
183-
for (int j = -1; j <= 1; j++) {
184-
if (j == 0) continue;
177+
int m = deque.size();
178+
while (m-- > 0) {
179+
String poll = deque.pollFirst();
180+
char[] pcs = poll.toCharArray();
181+
int step = cur.get(poll);
182+
// 枚举替换哪个字符
183+
for (int i = 0; i < 4; i++) {
184+
// 能「正向转」也能「反向转」,这里直接枚举偏移量 [-1,1] 然后跳过 0
185+
for (int j = -1; j <= 1; j++) {
186+
if (j == 0) continue;
185187

186-
// 求得替换字符串 str
187-
int origin = pcs[i] - '0';
188-
int next = (origin + j) % 10;
189-
if (next == -1) next = 9;
188+
// 求得替换字符串 str
189+
int origin = pcs[i] - '0';
190+
int next = (origin + j) % 10;
191+
if (next == -1) next = 9;
190192

191-
char[] clone = pcs.clone();
192-
clone[i] = (char)(next + '0');
193-
String str = String.valueOf(clone);
193+
char[] clone = pcs.clone();
194+
clone[i] = (char)(next + '0');
195+
String str = String.valueOf(clone);
194196

195-
if (set.contains(str)) continue;
196-
if (cur.containsKey(str)) continue;
197-
198-
// 如果在「另一方向」找到过,说明找到了最短路,否则加入队列
199-
if (other.containsKey(str)) {
200-
return step + 1 + other.get(str);
201-
} else {
202-
deque.addLast(str);
203-
cur.put(str, step + 1);
197+
if (set.contains(str)) continue;
198+
if (cur.containsKey(str)) continue;
199+
200+
// 如果在「另一方向」找到过,说明找到了最短路,否则加入队列
201+
if (other.containsKey(str)) {
202+
return step + 1 + other.get(str);
203+
} else {
204+
deque.addLast(str);
205+
cur.put(str, step + 1);
206+
}
204207
}
205208
}
206209
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /