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 53f1134

Browse files
committed
solve 752.打开转盘锁
1 parent d1ca368 commit 53f1134

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

‎zh/752.打开转盘锁.java‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* @lc app=leetcode.cn id=752 lang=java
3+
*
4+
* [752] 打开转盘锁
5+
*
6+
* https://leetcode-cn.com/problems/open-the-lock/description/
7+
*
8+
* algorithms
9+
* Medium (49.26%)
10+
* Likes: 118
11+
* Dislikes: 0
12+
* Total Accepted: 16.7K
13+
* Total Submissions: 34.2K
14+
* Testcase Example: '["0201","0101","0102","1212","2002"]\n"0202"'
15+
*
16+
* 你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8',
17+
* '9' 。每个拨轮可以自由旋转:例如把 '9' 变为 '0','0' 变为 '9' 。每次旋转都只能旋转一个拨轮的一位数字。
18+
*
19+
* 锁的初始数字为 '0000' ,一个代表四个拨轮的数字的字符串。
20+
*
21+
* 列表 deadends 包含了一组死亡数字,一旦拨轮的数字和列表里的任何一个元素相同,这个锁将会被永久锁定,无法再被旋转。
22+
*
23+
* 字符串 target 代表可以解锁的数字,你需要给出最小的旋转次数,如果无论如何不能解锁,返回 -1。
24+
*
25+
*
26+
*
27+
* 示例 1:
28+
*
29+
*
30+
* 输入:deadends = ["0201","0101","0102","1212","2002"], target = "0202"
31+
* 输出:6
32+
* 解释:
33+
* 可能的移动序列为 "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202"。
34+
* 注意 "0000" -> "0001" -> "0002" -> "0102" -> "0202" 这样的序列是不能解锁的,
35+
* 因为当拨动到 "0102" 时这个锁就会被锁定。
36+
*
37+
*
38+
* 示例 2:
39+
*
40+
*
41+
* 输入: deadends = ["8888"], target = "0009"
42+
* 输出:1
43+
* 解释:
44+
* 把最后一位反向旋转一次即可 "0000" -> "0009"。
45+
*
46+
*
47+
* 示例 3:
48+
*
49+
*
50+
* 输入: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"],
51+
* target = "8888"
52+
* 输出:-1
53+
* 解释:
54+
* 无法旋转到目标数字且不被锁定。
55+
*
56+
*
57+
* 示例 4:
58+
*
59+
*
60+
* 输入: deadends = ["0000"], target = "8888"
61+
* 输出:-1
62+
*
63+
*
64+
*
65+
*
66+
* 提示:
67+
*
68+
*
69+
* 死亡列表 deadends 的长度范围为 [1, 500]。
70+
* 目标数字 target 不会在 deadends 之中。
71+
* 每个 deadends 和 target 中的字符串的数字会在 10,000 个可能的情况 '0000' 到 '9999' 中产生。
72+
*
73+
*
74+
*/
75+
76+
// @lc code=start
77+
class Solution {
78+
public int openLock(String[] deadends, String target) {
79+
Set<String> deads = new HashSet<>();
80+
for (String deadend : deadends) {
81+
deads.add(deadend);
82+
}
83+
84+
Queue<String> queue = new LinkedList<>();
85+
Set<String> visited = new HashSet<>();
86+
queue.offer("0000");
87+
visited.add("0000");
88+
89+
int step = 0;
90+
91+
while (!queue.isEmpty()) {
92+
int size = queue.size();
93+
94+
for (int i = 0; i < size; i++) {
95+
String cur = queue.poll();
96+
97+
if (deads.contains(cur)) {
98+
continue;
99+
}
100+
if (cur.equals(target)) {
101+
return step;
102+
}
103+
104+
for (int j = 0; j < 4; j++) {
105+
String plus = plusOne(cur, j);
106+
if (!visited.contains(plus)) {
107+
queue.offer(plus);
108+
visited.add(plus);
109+
}
110+
String minus = minusOne(cur, j);
111+
if (!visited.contains(minus)) {
112+
queue.offer(minus);
113+
visited.add(minus);
114+
}
115+
}
116+
}
117+
118+
step++;
119+
}
120+
121+
return -1;
122+
}
123+
124+
public String plusOne(String str, int i) {
125+
char[] strArray = str.toCharArray();
126+
if (strArray[i] == '9') {
127+
strArray[i] = '0';
128+
} else {
129+
strArray[i] += 1;
130+
}
131+
return new String(strArray);
132+
}
133+
134+
public String minusOne(String str, int i) {
135+
char[] strArray = str.toCharArray();
136+
if (strArray[i] == '0') {
137+
strArray[i] = '9';
138+
} else {
139+
strArray[i] -= 1;
140+
}
141+
return new String(strArray);
142+
}
143+
}
144+
// @lc code=end
145+

0 commit comments

Comments
(0)

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