Leetcode Note: Go - Find Target Indices After Sorting Array

Find Target Indices After Sorting Array - LeetCode
https://leetcode.com/problems/find-target-indices-after-sorting-array/

  • Go 言語で取り組んだメモ

回答

Go O(n) one pass - Find Target Indices After Sorting Array - LeetCode
https://leetcode.com/problems/find-target-indices-after-sorting-array/solutions/2215861/go-o-n-one-pass/

func targetIndices(nums []int, target int) []int {
    var res []int
    
    count, left := 0, 0
    
    for i := 0; i < len(nums); i++ {
        if nums[i] == target {
            count++
        }
        
        if nums[i] < target {
            left++
        }
    }
    
    for i := 0; i < count; i++ {
        res = append(res, left)
        left++
    }
    
    return res
}