52
52
53
53
<!-- 这里可写通用的实现逻辑 -->
54
54
55
+ ** 方法一:枚举**
56
+
57
+ 我们在 $[ 0, n)$ 的范围内枚举下标 $i$,对于每个下标 $i$,我们在 $[ 0, n)$ 的范围内枚举下标 $j$,如果 $|i - j| \leq k$ 且 $nums[ j] == key$,那么 $i$ 就是一个 K 近邻下标,我们将 $i$ 加入答案数组中,然后跳出内层循环,枚举下一个下标 $i$。
58
+
59
+ 时间复杂度 $O(n^2)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
60
+
61
+ ** 方法二:预处理 + 二分查找**
62
+
63
+ 我们可以预处理得到所有等于 $key$ 的元素的下标,记录在数组 $idx$ 中。数组 $idx$ 中的所有下标元素是按照升序排列的,
64
+
65
+ 接下来,我们枚举下标 $i$,对于每个下标 $i$,我们可以使用二分查找的方法在数组 $idx$ 中查找 $[ i - k, i + k] $ 范围内的元素,如果存在元素,那么 $i$ 就是一个 K 近邻下标,我们将 $i$ 加入答案数组中。
66
+
67
+ 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
68
+
69
+ ** 方法三:双指针**
70
+
71
+ 我们枚举下标 $i$,用一个指针 $j$ 指向满足 $j \geq i - k$ 且 $nums[ j] = key$ 的最小下标,如果 $j$ 存在且 $j \leq i + k$,那么 $i$ 就是一个 K 近邻下标,我们将 $i$ 加入答案数组中。
72
+
73
+ 时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
74
+
55
75
<!-- tabs:start -->
56
76
57
77
### ** Python3**
@@ -64,10 +84,34 @@ class Solution:
64
84
ans = []
65
85
n = len (nums)
66
86
for i in range (n):
67
- for j in range (n):
68
- if abs (i - j) <= k and nums[j] == key:
69
- ans.append(i)
70
- break
87
+ if any (abs (i - j) <= k and nums[j] == key for j in range (n)):
88
+ ans.append(i)
89
+ return ans
90
+ ```
91
+
92
+ ``` python
93
+ class Solution :
94
+ def findKDistantIndices (self , nums : List[int ], key : int , k : int ) -> List[int ]:
95
+ idx = [i for i, x in enumerate (nums) if x == key]
96
+ ans = []
97
+ for i in range (len (nums)):
98
+ l = bisect_left(idx, i - k)
99
+ r = bisect_right(idx, i + k) - 1
100
+ if l <= r:
101
+ ans.append(i)
102
+ return ans
103
+ ```
104
+
105
+ ``` python
106
+ class Solution :
107
+ def findKDistantIndices (self , nums : List[int ], key : int , k : int ) -> List[int ]:
108
+ ans = []
109
+ j, n = 0 , len (nums)
110
+ for i in range (n):
111
+ while j < i - k or (j < n and nums[j] != key):
112
+ j += 1
113
+ if j < n and j <= (i + k):
114
+ ans.append(i)
71
115
return ans
72
116
```
73
117
@@ -93,22 +137,45 @@ class Solution {
93
137
}
94
138
```
95
139
96
- ### ** TypeScript**
140
+ ``` java
141
+ class Solution {
142
+ public List<Integer > findKDistantIndices (int [] nums , int key , int k ) {
143
+ List<Integer > idx = new ArrayList<> ();
144
+ for (int i = 0 ; i < nums. length; i++ ) {
145
+ if (nums[i] == key) {
146
+ idx. add(i);
147
+ }
148
+ }
149
+ List<Integer > ans = new ArrayList<> ();
150
+ for (int i = 0 ; i < nums. length; ++ i) {
151
+ int l = Collections . binarySearch(idx, i - k);
152
+ int r = Collections . binarySearch(idx, i + k + 1 );
153
+ l = l < 0 ? - l - 1 : l;
154
+ r = r < 0 ? - r - 2 : r - 1 ;
155
+ if (l <= r) {
156
+ ans. add(i);
157
+ }
158
+ }
159
+ return ans;
160
+ }
161
+ }
162
+ ```
97
163
98
- ``` ts
99
- function findKDistantIndices(nums : number [], key : number , k : number ): number [] {
100
- const n = nums .length ;
101
- let ans = [];
102
- for (let j = 0 ; j < n ; j ++ ) {
103
- if (nums [j ] == key ) {
104
- for (let i = j - k ; i <= j + k ; i ++ ) {
105
- if (i >= 0 && i < n && ! ans .includes (i )) {
106
- ans .push (i );
107
- }
164
+ ``` java
165
+ class Solution {
166
+ public List<Integer > findKDistantIndices (int [] nums , int key , int k ) {
167
+ int n = nums. length;
168
+ List<Integer > ans = new ArrayList<> ();
169
+ for (int i = 0 , j = 0 ; i < n; ++ i) {
170
+ while (j < i - k || (j < n && nums[j] != key)) {
171
+ ++ j;
172
+ }
173
+ if (j < n && j <= i + k) {
174
+ ans. add(i);
108
175
}
109
176
}
177
+ return ans;
110
178
}
111
- return ans ;
112
179
}
113
180
```
114
181
@@ -133,15 +200,56 @@ public:
133
200
};
134
201
```
135
202
203
+ ```cpp
204
+ class Solution {
205
+ public:
206
+ vector<int> findKDistantIndices(vector<int>& nums, int key, int k) {
207
+ vector<int> idx;
208
+ int n = nums.size();
209
+ for (int i = 0; i < n; ++i) {
210
+ if (nums[i] == key) {
211
+ idx.push_back(i);
212
+ }
213
+ }
214
+ vector<int> ans;
215
+ for (int i = 0; i < n; ++i) {
216
+ auto it1 = lower_bound(idx.begin(), idx.end(), i - k);
217
+ auto it2 = upper_bound(idx.begin(), idx.end(), i + k) - 1;
218
+ if (it1 <= it2) {
219
+ ans.push_back(i);
220
+ }
221
+ }
222
+ return ans;
223
+ }
224
+ };
225
+ ```
226
+
227
+ ``` cpp
228
+ class Solution {
229
+ public:
230
+ vector<int > findKDistantIndices(vector<int >& nums, int key, int k) {
231
+ int n = nums.size();
232
+ vector<int > ans;
233
+ for (int i = 0, j = 0; i < n; ++i) {
234
+ while (j < i - k || (j < n && nums[ j] != key)) {
235
+ ++j;
236
+ }
237
+ if (j < n && j <= i + k) {
238
+ ans.push_back(i);
239
+ }
240
+ }
241
+ return ans;
242
+ }
243
+ };
244
+ ```
245
+
136
246
### **Go**
137
247
138
248
```go
139
- func findKDistantIndices(nums []int, key int, k int) []int {
140
- n := len(nums)
141
- var ans []int
142
- for i := 0; i < n; i++ {
143
- for j, v := range nums {
144
- if abs(i-j) <= k && v == key {
249
+ func findKDistantIndices(nums []int, key int, k int) (ans []int) {
250
+ for i := range nums {
251
+ for j, x := range nums {
252
+ if abs(i-j) <= k && x == key {
145
253
ans = append(ans, i)
146
254
break
147
255
}
@@ -158,6 +266,107 @@ func abs(x int) int {
158
266
}
159
267
```
160
268
269
+ ``` go
270
+ func findKDistantIndices (nums []int , key int , k int ) (ans []int ) {
271
+ idx := []int {}
272
+ for i , x := range nums {
273
+ if x == key {
274
+ idx = append (idx, i)
275
+ }
276
+ }
277
+ for i := range nums {
278
+ l := sort.SearchInts (idx, i-k)
279
+ r := sort.SearchInts (idx, i+k+1 ) - 1
280
+ if l <= r {
281
+ ans = append (ans, i)
282
+ }
283
+ }
284
+ return
285
+ }
286
+ ```
287
+
288
+ ``` go
289
+ func findKDistantIndices (nums []int , key int , k int ) (ans []int ) {
290
+ n := len (nums)
291
+ for i , j := 0 , 0 ; i < n; i++ {
292
+ for j < i-k || (j < n && nums[j] != key) {
293
+ j++
294
+ }
295
+ if j < n && j <= i+k {
296
+ ans = append (ans, i)
297
+ }
298
+ }
299
+ return
300
+ }
301
+ ```
302
+
303
+ ### ** TypeScript**
304
+
305
+ ``` ts
306
+ function findKDistantIndices(nums : number [], key : number , k : number ): number [] {
307
+ const n = nums .length ;
308
+ const ans: number [] = [];
309
+ for (let i = 0 ; i < n ; ++ i ) {
310
+ for (let j = 0 ; j < n ; ++ j ) {
311
+ if (Math .abs (i - j ) <= k && nums [j ] === key ) {
312
+ ans .push (i );
313
+ break ;
314
+ }
315
+ }
316
+ }
317
+ return ans ;
318
+ }
319
+ ```
320
+
321
+ ``` ts
322
+ function findKDistantIndices(nums : number [], key : number , k : number ): number [] {
323
+ const n = nums .length ;
324
+ const idx: number [] = [];
325
+ for (let i = 0 ; i < n ; i ++ ) {
326
+ if (nums [i ] === key ) {
327
+ idx .push (i );
328
+ }
329
+ }
330
+ const search = (x : number ): number => {
331
+ let [l, r] = [0 , idx .length ];
332
+ while (l < r ) {
333
+ const mid = (l + r ) >> 1 ;
334
+ if (idx [mid ] >= x ) {
335
+ r = mid ;
336
+ } else {
337
+ l = mid + 1 ;
338
+ }
339
+ }
340
+ return l ;
341
+ };
342
+ const ans: number [] = [];
343
+ for (let i = 0 ; i < n ; ++ i ) {
344
+ const l = search (i - k );
345
+ const r = search (i + k + 1 ) - 1 ;
346
+ if (l <= r ) {
347
+ ans .push (i );
348
+ }
349
+ }
350
+ return ans ;
351
+ }
352
+ ```
353
+
354
+ ``` ts
355
+ function findKDistantIndices(nums : number [], key : number , k : number ): number [] {
356
+ const n = nums .length ;
357
+ const ans: number [] = [];
358
+ for (let i = 0 , j = 0 ; i < n ; ++ i ) {
359
+ while (j < i - k || (j < n && nums [j ] !== key )) {
360
+ ++ j ;
361
+ }
362
+ if (j < n && j <= i + k ) {
363
+ ans .push (i );
364
+ }
365
+ }
366
+ return ans ;
367
+ }
368
+ ```
369
+
161
370
### ** ...**
162
371
163
372
```
0 commit comments