Skip to content

Commit 50384df

Browse files
committed
feat: add solutions to lc problem: No.1848
No.1848.Minimum Distance to the Target Element
1 parent d87c6ce commit 50384df

File tree

6 files changed

+109
-39
lines changed

6 files changed

+109
-39
lines changed

Diff for: solution/1800-1899/1848.Minimum Distance to the Target Element/README.md

+42-13
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:一次遍历**
57+
58+
遍历数组,找到所有等于 `target` 的下标,然后计算 `abs(i - start)`,取最小值即可。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
@@ -62,11 +68,11 @@
6268
```python
6369
class Solution:
6470
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
65-
res = inf
66-
for i, num in enumerate(nums):
67-
if num == target:
68-
res = min(res, abs(i - start))
69-
return res
71+
ans = inf
72+
for i, x in enumerate(nums):
73+
if x == target:
74+
ans = min(ans, abs(i - start))
75+
return ans
7076
```
7177

7278
### **Java**
@@ -76,13 +82,14 @@ class Solution:
7682
```java
7783
class Solution {
7884
public int getMinDistance(int[] nums, int target, int start) {
79-
int res = Integer.MAX_VALUE;
80-
for (int i = 0; i < nums.length; ++i) {
85+
int n = nums.length;
86+
int ans = n;
87+
for (int i = 0; i < n; ++i) {
8188
if (nums[i] == target) {
82-
res = Math.min(res, Math.abs(i - start));
89+
ans = Math.min(ans, Math.abs(i - start));
8390
}
8491
}
85-
return res;
92+
return ans;
8693
}
8794
}
8895
```
@@ -93,17 +100,39 @@ class Solution {
93100
class Solution {
94101
public:
95102
int getMinDistance(vector<int>& nums, int target, int start) {
96-
int res = nums.size();
97-
for (int i = 0; i < nums.size(); ++i) {
103+
int n = nums.size();
104+
int ans = n;
105+
for (int i = 0; i < n; ++i) {
98106
if (nums[i] == target) {
99-
res = min(res, abs(i - start));
107+
ans = min(ans, abs(i - start));
100108
}
101109
}
102-
return res;
110+
return ans;
103111
}
104112
};
105113
```
106114
115+
### **Go**
116+
117+
```go
118+
func getMinDistance(nums []int, target int, start int) int {
119+
ans := 1 << 30
120+
for i, x := range nums {
121+
if t := abs(i - start); x == target && t < ans {
122+
ans = t
123+
}
124+
}
125+
return ans
126+
}
127+
128+
func abs(x int) int {
129+
if x < 0 {
130+
return -x
131+
}
132+
return x
133+
}
134+
```
135+
107136
### **...**
108137

109138
```

Diff for: solution/1800-1899/1848.Minimum Distance to the Target Element/README_EN.md

+36-13
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,26 @@
5454
```python
5555
class Solution:
5656
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
57-
res = inf
58-
for i, num in enumerate(nums):
59-
if num == target:
60-
res = min(res, abs(i - start))
61-
return res
57+
ans = inf
58+
for i, x in enumerate(nums):
59+
if x == target:
60+
ans = min(ans, abs(i - start))
61+
return ans
6262
```
6363

6464
### **Java**
6565

6666
```java
6767
class Solution {
6868
public int getMinDistance(int[] nums, int target, int start) {
69-
int res = Integer.MAX_VALUE;
70-
for (int i = 0; i < nums.length; ++i) {
69+
int n = nums.length;
70+
int ans = n;
71+
for (int i = 0; i < n; ++i) {
7172
if (nums[i] == target) {
72-
res = Math.min(res, Math.abs(i - start));
73+
ans = Math.min(ans, Math.abs(i - start));
7374
}
7475
}
75-
return res;
76+
return ans;
7677
}
7778
}
7879
```
@@ -83,17 +84,39 @@ class Solution {
8384
class Solution {
8485
public:
8586
int getMinDistance(vector<int>& nums, int target, int start) {
86-
int res = nums.size();
87-
for (int i = 0; i < nums.size(); ++i) {
87+
int n = nums.size();
88+
int ans = n;
89+
for (int i = 0; i < n; ++i) {
8890
if (nums[i] == target) {
89-
res = min(res, abs(i - start));
91+
ans = min(ans, abs(i - start));
9092
}
9193
}
92-
return res;
94+
return ans;
9395
}
9496
};
9597
```
9698
99+
### **Go**
100+
101+
```go
102+
func getMinDistance(nums []int, target int, start int) int {
103+
ans := 1 << 30
104+
for i, x := range nums {
105+
if t := abs(i - start); x == target && t < ans {
106+
ans = t
107+
}
108+
}
109+
return ans
110+
}
111+
112+
func abs(x int) int {
113+
if x < 0 {
114+
return -x
115+
}
116+
return x
117+
}
118+
```
119+
97120
### **...**
98121

99122
```
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Solution {
22
public:
33
int getMinDistance(vector<int>& nums, int target, int start) {
4-
int res = nums.size();
5-
for (int i = 0; i < nums.size(); ++i) {
4+
int n = nums.size();
5+
int ans = n;
6+
for (int i = 0; i < n; ++i) {
67
if (nums[i] == target) {
7-
res = min(res, abs(i - start));
8+
ans = min(ans, abs(i - start));
89
}
910
}
10-
return res;
11+
return ans;
1112
}
1213
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func getMinDistance(nums []int, target int, start int) int {
2+
ans := 1 << 30
3+
for i, x := range nums {
4+
if t := abs(i - start); x == target && t < ans {
5+
ans = t
6+
}
7+
}
8+
return ans
9+
}
10+
11+
func abs(x int) int {
12+
if x < 0 {
13+
return -x
14+
}
15+
return x
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution {
22
public int getMinDistance(int[] nums, int target, int start) {
3-
int res = Integer.MAX_VALUE;
4-
for (int i = 0; i < nums.length; ++i) {
3+
int n = nums.length;
4+
int ans = n;
5+
for (int i = 0; i < n; ++i) {
56
if (nums[i] == target) {
6-
res = Math.min(res, Math.abs(i - start));
7+
ans = Math.min(ans, Math.abs(i - start));
78
}
89
}
9-
return res;
10+
return ans;
1011
}
1112
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def getMinDistance(self, nums: List[int], target: int, start: int) -> int:
3-
res = inf
4-
for i, num in enumerate(nums):
5-
if num == target:
6-
res = min(res, abs(i - start))
7-
return res
3+
ans = inf
4+
for i, x in enumerate(nums):
5+
if x == target:
6+
ans = min(ans, abs(i - start))
7+
return ans

0 commit comments

Comments
 (0)