Skip to content

Commit 22ff9e0

Browse files
committed
Create find-all-k-distant-indices-in-an-array.go
1 parent 7deea04 commit 22ff9e0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode_solutions_golang
2+
3+
import (
4+
"math"
5+
"sort"
6+
)
7+
8+
//https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/
9+
func findKDistantIndices(nums []int, key int, k int) []int {
10+
result := make(map[int]bool, 0)
11+
size := len(nums)
12+
for i := 0; i < size; i++ {
13+
for j := 0; j < size; j++ {
14+
distant := int(math.Abs(float64(i - j)))
15+
if distant <= k && nums[j] == key {
16+
result[i] = true
17+
}
18+
}
19+
}
20+
resultSlice := make([]int, 0)
21+
for key, _ := range result {
22+
resultSlice = append(resultSlice, key)
23+
}
24+
sort.Ints(resultSlice)
25+
return resultSlice
26+
}

0 commit comments

Comments
 (0)