Leetcode Note: Go - Find All K Distant Indices in an Array
Find All K-Distant Indices in an Array - LeetCode
https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/
- Go 言語で取り組んだメモ
 
回答
Find All K-Distant Indices in an Array - LeetCode
https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/solutions/2541334/golang-o-n/
func findKDistantIndices(nums []int, key int, k int) []int {
    var res []int
    resMap := make(map[int]struct{})
    for i, v := range nums {
        if v == key {         
            for j := i - k; j <= i + k; j++ {
                if j < 0 || j >= len(nums) {
                    continue
                }
                if _, ok := resMap[j]; !ok {
                    resMap[j] = struct{}{}
                    res = append(res, j)   
                }
            }
        }
    }
    
    return res
}