mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 19:31:42 +08:00
54 lines
2.5 KiB
HTML
54 lines
2.5 KiB
HTML
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/11/leetcode_keyboard.png" /></p>
|
||
|
||
<p>二指输入法定制键盘在 <strong>X-Y</strong> 平面上的布局如上图所示,其中每个大写英文字母都位于某个坐标处。</p>
|
||
|
||
<ul>
|
||
<li>例如字母 <strong>A</strong> 位于坐标 <strong>(0,0)</strong>,字母 <strong>B</strong> 位于坐标 <strong>(0,1)</strong>,字母 <strong>P</strong> 位于坐标 <strong>(2,3)</strong> 且字母 <strong>Z</strong> 位于坐标 <strong>(4,1)</strong>。</li>
|
||
</ul>
|
||
|
||
<p>给你一个待输入字符串 <code>word</code>,请你计算并返回在仅使用两根手指的情况下,键入该字符串需要的最小移动总距离。</p>
|
||
|
||
<p>坐标<code> <strong>(x<sub>1</sub>,y<sub>1</sub>)</strong> </code>和 <code><strong>(x<sub>2</sub>,y<sub>2</sub>)</strong></code> 之间的 <strong>距离</strong> 是 <code><strong>|x<sub>1</sub> - x<sub>2</sub>| + |y<sub>1</sub> - y<sub>2</sub>|</strong></code>。 </p>
|
||
|
||
<p><strong>注意</strong>,两根手指的起始位置是零代价的,不计入移动总距离。你的两根手指的起始位置也不必从首字母或者前两个字母开始。</p>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>示例 1:</strong></p>
|
||
|
||
<pre>
|
||
<strong>输入:</strong>word = "CAKE"
|
||
<strong>输出:</strong>3
|
||
<strong>解释:
|
||
</strong>使用两根手指输入 "CAKE" 的最佳方案之一是:
|
||
手指 1 在字母 'C' 上 -> 移动距离 = 0
|
||
手指 1 在字母 'A' 上 -> 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2
|
||
手指 2 在字母 'K' 上 -> 移动距离 = 0
|
||
手指 2 在字母 'E' 上 -> 移动距离 = 从字母 'K' 到字母 'E' 的距离 = 1
|
||
总距离 = 3
|
||
</pre>
|
||
|
||
<p><strong>示例 2:</strong></p>
|
||
|
||
<pre>
|
||
<strong>输入:</strong>word = "HAPPY"
|
||
<strong>输出:</strong>6
|
||
<strong>解释: </strong>
|
||
使用两根手指输入 "HAPPY" 的最佳方案之一是:
|
||
手指 1 在字母 'H' 上 -> 移动距离 = 0
|
||
手指 1 在字母 'A' 上 -> 移动距离 = 从字母 'H' 到字母 'A' 的距离 = 2
|
||
手指 2 在字母 'P' 上 -> 移动距离 = 0
|
||
手指 2 在字母 'P' 上 -> 移动距离 = 从字母 'P' 到字母 'P' 的距离 = 0
|
||
手指 1 在字母 'Y' 上 -> 移动距离 = 从字母 'A' 到字母 'Y' 的距离 = 4
|
||
总距离 = 6
|
||
</pre>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>2 <= word.length <= 300</code></li>
|
||
<li>每个 <code>word[i]</code> 都是一个大写英文字母。</li>
|
||
</ul>
|