1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/打开转盘锁 [open-the-lock].html
2022-03-29 12:43:11 +08:00

49 lines
2.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字 <code>'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'</code> 。每个拨轮可以自由旋转:例如把 <code>'9'</code> 变为&nbsp;<code>'0'</code><code>'0'</code> 变为 <code>'9'</code> 。每次旋转都只能旋转一个拨轮的一位数字。</p>
<p>锁的初始数字为 <code>'0000'</code> ,一个代表四个拨轮的数字的字符串。</p>
<p>列表 <code>deadends</code> 包含了一组死亡数字,一旦拨轮的数字和列表里的任何一个元素相同,这个锁将会被永久锁定,无法再被旋转。</p>
<p>字符串 <code>target</code> 代表可以解锁的数字,你需要给出解锁需要的最小旋转次数,如果无论如何不能解锁,返回 <code>-1</code></p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>deadends = ["0201","0101","0102","1212","2002"], target = "0202"
<strong>输出:</strong>6
<strong>解释:</strong>
可能的移动序列为 "0000" -&gt; "1000" -&gt; "1100" -&gt; "1200" -&gt; "1201" -&gt; "1202" -&gt; "0202"。
注意 "0000" -&gt; "0001" -&gt; "0002" -&gt; "0102" -&gt; "0202" 这样的序列是不能解锁的,
因为当拨动到 "0102" 时这个锁就会被锁定。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> deadends = ["8888"], target = "0009"
<strong>输出:</strong>1
<strong>解释:</strong>把最后一位反向旋转一次即可 "0000" -&gt; "0009"。
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong> deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
<strong>输出:</strong>-1
<strong>解释:</strong>无法旋转到目标数字且不被锁定。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;=&nbsp;deadends.length &lt;= 500</code></li>
<li><code><font face="monospace">deadends[i].length == 4</font></code></li>
<li><code><font face="monospace">target.length == 4</font></code></li>
<li><code>target</code> <strong>不在</strong> <code>deadends</code> 之中</li>
<li><code>target</code><code>deadends[i]</code> 仅由若干位数字组成</li>
</ul>