Golang solution (6ms)


  • 0
    func combinationSum(nums []int, target int) (result [][]int) {
        sort.Ints(nums)
        combinationSumHelper(nums, []int{}, target, 0, 0, &result)
        return result
    }
    
    func combinationSumHelper(nums, subset []int, target, startIndex, sum int, result *[][]int) {
        if sum == target {
            *result = append(*result, append([]int{}, subset...))
            return
        }
        for i := startIndex; i < len(nums) && sum + nums[i] <= target; i++ {
            combinationSumHelper(nums, append(subset, nums[i]), target, i, sum + nums[i], result)
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.