1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-02-10 01:10:26 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/到目标字符串的最短距离 [shortest-distance-to-target-string-in-a-circular-array].html

51 lines
2.3 KiB
HTML
Raw Normal View History

2023-01-04 16:53:10 +08:00
<p>给你一个下标从 <strong>0</strong> 开始的 <strong>环形</strong> 字符串数组 <code>words</code> 和一个字符串 <code>target</code><strong>环形数组</strong> 意味着数组首尾相连。</p>
<ul>
<li>形式上, <code>words[i]</code> 的下一个元素是 <code>words[(i + 1) % n]</code> ,而 <code>words[i]</code> 的前一个元素是 <code>words[(i - 1 + n) % n]</code> ,其中 <code>n</code><code>words</code> 的长度。</li>
</ul>
<p><code>startIndex</code> 开始,你一次可以用 <code>1</code> 步移动到下一个或者前一个单词。</p>
<p>返回到达目标字符串 <code>target</code> 所需的最短距离。如果 <code>words</code> 中不存在字符串 <code>target</code> ,返回 <code>-1</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1
<strong>输出:</strong>1
<strong>解释:</strong>从下标 1 开始,可以经由以下步骤到达 "hello"
- 向右移动 3 个单位,到达下标 4 。
- 向左移动 2 个单位,到达下标 4 。
- 向右移动 4 个单位,到达下标 0 。
- 向左移动 1 个单位,到达下标 0 。
到达 "hello" 的最短距离是 1 。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>words = ["a","b","leetcode"], target = "leetcode", startIndex = 0
<strong>输出:</strong>1
<strong>解释:</strong>从下标 0 开始,可以经由以下步骤到达 "leetcode"
- 向右移动 2 个单位,到达下标 3 。
- 向左移动 1 个单位,到达下标 3 。
到达 "leetcode" 的最短距离是 1 。</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>words = ["i","eat","leetcode"], target = "ate", startIndex = 0
<strong>输出:</strong>-1
<strong>解释:</strong>因为 words 中不存在字符串 "ate" ,所以返回 -1 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= words.length &lt;= 100</code></li>
<li><code>1 &lt;= words[i].length &lt;= 100</code></li>
<li><code>words[i]</code><code>target</code> 仅由小写英文字母组成</li>
<li><code>0 &lt;= startIndex &lt; words.length</code></li>
</ul>