|
| 1 | +--- |
| 2 | +title: 1848.到目标元素的最小距离:数组遍历(附python一行版) |
| 3 | +date: 2026-04-13 22:00:02 |
| 4 | +tags: [题解, LeetCode, 简单, 数组] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】1848.到目标元素的最小距离:数组遍历(附python一行版) |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/minimum-distance-to-the-target-element/](https://leetcode.cn/problems/minimum-distance-to-the-target-element/) |
| 11 | + |
| 12 | +<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> |
| 13 | + |
| 14 | +<p>返回 <code>abs(i - start)</code> 。</p> |
| 15 | + |
| 16 | +<p>题目数据保证 <code>target</code> 存在于 <code>nums</code> 中。</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | + |
| 20 | +<p><strong>示例 1:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>输入:</strong>nums = [1,2,3,4,5], target = 5, start = 3 |
| 24 | +<strong>输出:</strong>1 |
| 25 | +<strong>解释:</strong>nums[4] = 5 是唯一一个等于 target 的值,所以答案是 abs(4 - 3) = 1 。 |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong>示例 2:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>输入:</strong>nums = [1], target = 1, start = 0 |
| 32 | +<strong>输出:</strong>0 |
| 33 | +<strong>解释:</strong>nums[0] = 1 是唯一一个等于 target 的值,所以答案是 abs(0 - 0) = 0 。 |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong>示例 3:</strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<strong>输入:</strong>nums = [1,1,1,1,1,1,1,1,1,1], target = 1, start = 0 |
| 40 | +<strong>输出:</strong>0 |
| 41 | +<strong>解释:</strong>nums 中的每个值都是 1 ,但 nums[0] 使 abs(i - start) 的结果得以最小化,所以答案是 abs(0 - 0) = 0 。 |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | + |
| 46 | +<p><strong>提示:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>1 <= nums.length <= 1000</code></li> |
| 50 | + <li><code>1 <= nums[i] <= 10<sup>4</sup></code></li> |
| 51 | + <li><code>0 <= start < nums.length</code></li> |
| 52 | + <li><code>target</code> 存在于 <code>nums</code> 中</li> |
| 53 | +</ul> |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +## 解题方法:遍历 |
| 58 | + |
| 59 | +遍历一遍数组,如果当前元素和`target`相等,就更新`ans`关于`abs(i - start)`的最小值。 |
| 60 | + |
| 61 | ++ 时间复杂度$O(len(nums))$ |
| 62 | ++ 空间复杂度$O(1)$ |
| 63 | + |
| 64 | +也可从$start$开始往左右遍历,遍历到一个就停止(需注意边界case)。 |
| 65 | + |
| 66 | +### AC代码 |
| 67 | + |
| 68 | +#### C++ |
| 69 | + |
| 70 | +```cpp |
| 71 | +/* |
| 72 | + * @LastEditTime: 2026-04-13 21:40:13 |
| 73 | + */ |
| 74 | +class Solution { |
| 75 | +public: |
| 76 | + int getMinDistance(vector<int>& nums, int target, int start) { |
| 77 | + int ans = nums.size(); |
| 78 | + for (int i = 0, n = nums.size(); i < n; i++) { |
| 79 | + if (nums[i] == target) { |
| 80 | + ans = min(ans, abs(i - start)); |
| 81 | + } |
| 82 | + } |
| 83 | + return ans; |
| 84 | + } |
| 85 | +}; |
| 86 | +``` |
| 87 | +
|
| 88 | +#### Python |
| 89 | +
|
| 90 | +```python |
| 91 | +''' |
| 92 | +LastEditTime: 2026-04-13 21:41:57 |
| 93 | +''' |
| 94 | +from typing import List |
| 95 | +
|
| 96 | +class Solution: |
| 97 | + def getMinDistance(self, nums: List[int], target: int, start: int) -> int: |
| 98 | + return min(abs(i - start) for i, v in enumerate(nums) if v == target) |
| 99 | +``` |
| 100 | + |
| 101 | +#### Java |
| 102 | + |
| 103 | +```java |
| 104 | +/* |
| 105 | + * @LastEditTime: 2026-04-13 21:44:45 |
| 106 | + */ |
| 107 | +class Solution { |
| 108 | + public int getMinDistance(int[] nums, int target, int start) { |
| 109 | + int ans = nums.length; |
| 110 | + for (int i = 0, n = nums.length; i < n; i++) { |
| 111 | + if (nums[i] == target) { |
| 112 | + ans = Math.min(ans, Math.abs(i - start)); |
| 113 | + } |
| 114 | + } |
| 115 | + return ans; |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +#### Go |
| 121 | + |
| 122 | +```go |
| 123 | +/* |
| 124 | + * @LastEditTime: 2026-04-13 21:44:01 |
| 125 | + */ |
| 126 | +package main |
| 127 | + |
| 128 | +func abs1848(a int) int { |
| 129 | + if a >= 0 { |
| 130 | + return a |
| 131 | + } |
| 132 | + return -a |
| 133 | +} |
| 134 | + |
| 135 | +func getMinDistance(nums []int, target int, start int) int { |
| 136 | + ans := len(nums) |
| 137 | + for i, v := range nums { |
| 138 | + if v == target { |
| 139 | + ans = min(ans, abs1848(i - start)) |
| 140 | + } |
| 141 | + } |
| 142 | + return ans |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +#### Rust |
| 147 | + |
| 148 | +```rust |
| 149 | +/* |
| 150 | + * @LastEditTime: 2026-04-13 21:46:02 |
| 151 | + */ |
| 152 | +impl Solution { |
| 153 | + pub fn get_min_distance(nums: Vec<i32>, target: i32, start: i32) -> i32 { |
| 154 | + let mut ans = nums.len() as i32; |
| 155 | + for i in 0..nums.len() { |
| 156 | + if nums[i] == target { |
| 157 | + ans = ans.min((i as i32 - start).abs()); |
| 158 | + } |
| 159 | + } |
| 160 | + ans |
| 161 | + } |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/160123059)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/04/13/LeetCode%201848.%E5%88%B0%E7%9B%AE%E6%A0%87%E5%85%83%E7%B4%A0%E7%9A%84%E6%9C%80%E5%B0%8F%E8%B7%9D%E7%A6%BB/)哦~ |
| 166 | +> |
| 167 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments