题目链接:https://leetcode-cn.com/problems/minimum-distance-to-the-target-element/
思路:水题不解释~~~
上代码:
class Solution {
public int getMinDistance(int[] nums, int target, int start) {
int minn = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
minn = Math.min(minn, Math.abs(i - start));
}
}
return minn;
}
}
本文介绍了一种解决LeetCode上寻找目标元素最小距离问题的方法。通过遍历数组找到目标值,并计算其与起始位置的距离,从而确定最小距离。
10万+

被折叠的 条评论
为什么被折叠?



