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)/到目标元素的最小距离 [minimum-distance-to-the-target-element].html
2022-03-29 12:43:11 +08:00

43 lines
1.6 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>给你一个整数数组 <code>nums</code> (下标 <strong>从 0 开始</strong> 计数)以及两个整数 <code>target</code><code>start</code> ,请你找出一个下标 <code>i</code> ,满足 <code>nums[i] == target</code><code>abs(i - start)</code> <strong>最小化</strong> 。注意:<code>abs(x)</code> 表示 <code>x</code> 的绝对值。</p>
<p>返回 <code>abs(i - start)</code></p>
<p>题目数据保证 <code>target</code> 存在于 <code>nums</code> 中。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,4,5], target = 5, start = 3
<strong>输出:</strong>1
<strong>解释:</strong>nums[4] = 5 是唯一一个等于 target 的值,所以答案是 abs(4 - 3) = 1 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1], target = 1, start = 0
<strong>输出:</strong>0
<strong>解释:</strong>nums[0] = 1 是唯一一个等于 target 的值,所以答案是 abs(0 - 0) = 0 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0
<strong>输出:</strong>0
<strong>解释:</strong>nums 中的每个值都是 1 ,但 nums[0] 使 abs(i - start) 的结果得以最小化,所以答案是 abs(0 - 0) = 0 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
<li><code>0 <= start < nums.length</code></li>
<li><code>target</code> 存在于 <code>nums</code></li>
</ul>