Skip to content

Commit de70cf3

Browse files
committed
update: 添加问题“1848.到目标元素的最小距离”的代码和题解 (#1495)
1848: AC.cpp+py+go+java+rust (#1494) cpp - AC,100.00%,56.90% py - AC,100.00%,57.69% go - AC,100.00%,86.36% java - AC,21.05%,75.44% rust - AC,100.00%,80.00% Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 312009b commit de70cf3

13 files changed

Lines changed: 309 additions & 25 deletions

.commitTitleExtra

Whitespace-only changes.

.commitmsg

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
3741: AC.cpp (#1490) - AC,26.07%,21.26%
1+
1848: AC.cpp+py+go+java+rust (#1494)
2+
3+
cpp - AC,100.00%,56.90%
4+
py - AC,100.00%,57.69%
5+
go - AC,100.00%,86.36%
6+
java - AC,21.05%,75.44%
7+
rust - AC,100.00%,80.00%
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-04-13 21:39:33
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-04-13 21:40:13
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int getMinDistance(vector<int>& nums, int target, int start) {
14+
int ans = nums.size();
15+
for (int i = 0, n = nums.size(); i < n; i++) {
16+
if (nums[i] == target) {
17+
ans = min(ans, abs(i - start));
18+
}
19+
}
20+
return ans;
21+
}
22+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-04-13 21:39:33
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-04-13 21:44:01
6+
*/
7+
package main
8+
9+
func abs1848(a int) int {
10+
if a >= 0 {
11+
return a
12+
}
13+
return -a
14+
}
15+
16+
func getMinDistance(nums []int, target int, start int) int {
17+
ans := len(nums)
18+
for i, v := range nums {
19+
if v == target {
20+
ans = min(ans, abs1848(i - start))
21+
}
22+
}
23+
return ans
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-04-13 21:39:33
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-04-13 21:44:45
6+
*/
7+
class Solution {
8+
public int getMinDistance(int[] nums, int target, int start) {
9+
int ans = nums.length;
10+
for (int i = 0, n = nums.length; i < n; i++) {
11+
if (nums[i] == target) {
12+
ans = Math.min(ans, Math.abs(i - start));
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2026-04-13 21:39:33
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2026-04-13 21:41:57
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
11+
return min(abs(i - start) for i, v in enumerate(nums) if v == target)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-04-13 21:39:33
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-04-13 21:46:02
6+
*/
7+
impl Solution {
8+
pub fn get_min_distance(nums: Vec<i32>, target: i32, start: i32) -> i32 {
9+
let mut ans = nums.len() as i32;
10+
for i in 0..nums.len() {
11+
if nums[i] == target {
12+
ans = ans.min((i as i32 - start).abs());
13+
}
14+
}
15+
ans
16+
}
17+
}

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
pub struct Solution;
88

9-
include!("2946-matrix-similarity-after-cyclic-shifts.rs"); // 这个fileName是会被脚本替换掉的
9+
include!("1848-minimum-distance-to-the-target-element.rs"); // 这个fileName是会被脚本替换掉的
1010

1111
#[derive(Debug, PartialEq, Eq)]
1212
pub struct TreeNode {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@
697697
|1832.判断句子是否为全字母句|简单|<a href="https://leetcode.cn/problems/check-if-the-sentence-is-pangram/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/12/13/LeetCode%201832.%E5%88%A4%E6%96%AD%E5%8F%A5%E5%AD%90%E6%98%AF%E5%90%A6%E4%B8%BA%E5%85%A8%E5%AD%97%E6%AF%8D%E5%8F%A5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128304160" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-the-sentence-is-pangram/solutions/2018952/letmefly-1832pan-duan-ju-zi-shi-fou-wei-fd4l0/" target="_blank">LeetCode题解</a>|
698698
|1845.座位预约管理系统|中等|<a href="https://leetcode.cn/problems/seat-reservation-manager/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/02/LeetCode%201845.%E5%BA%A7%E4%BD%8D%E9%A2%84%E7%BA%A6%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142686842" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/seat-reservation-manager/solutions/2937537/letmefly-1845zuo-wei-yu-yue-guan-li-xi-t-qqxe/" target="_blank">LeetCode题解</a>|
699699
|1847.最近的房间|困难|<a href="https://leetcode.cn/problems/closest-room/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/17/LeetCode%201847.%E6%9C%80%E8%BF%91%E7%9A%84%E6%88%BF%E9%97%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144524587" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/closest-room/solutions/3022258/letmefly-1847zui-jin-de-fang-jian-you-xu-9aeg/" target="_blank">LeetCode题解</a>|
700+
|1848.到目标元素的最小距离|简单|<a href="https://leetcode.cn/problems/minimum-distance-to-the-target-element/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/160123059" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-distance-to-the-target-element/solutions/3951021/letmefly-1848dao-mu-biao-yuan-su-de-zui-q8qsh/" target="_blank">LeetCode题解</a>|
700701
|1857.有向图中最大颜色值|困难|<a href="https://leetcode.cn/problems/largest-color-value-in-a-directed-graph/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/26/LeetCode%201857.%E6%9C%89%E5%90%91%E5%9B%BE%E4%B8%AD%E6%9C%80%E5%A4%A7%E9%A2%9C%E8%89%B2%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148242411" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-color-value-in-a-directed-graph/solutions/3686868/letmefly-1857you-xiang-tu-zhong-zui-da-y-6wj6/" target="_blank">LeetCode题解</a>|
701702
|1863.找出所有子集的异或总和再求和|简单|<a href="https://leetcode.cn/problems/sum-of-all-subset-xor-totals/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/06/LeetCode%201863.%E6%89%BE%E5%87%BA%E6%89%80%E6%9C%89%E5%AD%90%E9%9B%86%E7%9A%84%E5%BC%82%E6%88%96%E6%80%BB%E5%92%8C%E5%86%8D%E6%B1%82%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147027120" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/sum-of-all-subset-xor-totals/solutions/3642017/letmefly-1863zhao-chu-suo-you-zi-ji-de-y-a02c/" target="_blank">LeetCode题解</a>|
702703
|1870.准时到达的列车最小时速|中等|<a href="https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/02/LeetCode%201870.%E5%87%86%E6%97%B6%E5%88%B0%E8%BE%BE%E7%9A%84%E5%88%97%E8%BD%A6%E6%9C%80%E5%B0%8F%E6%97%B6%E9%80%9F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142680612" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/solutions/2937033/letmefly-1870zhun-shi-dao-da-de-lie-che-liv8g/" target="_blank">LeetCode题解</a>|
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)