HTMLify

LeetCode - Two Sum II - Input Array Is Sorted - Go
Views: 1 | Author: abh
func twoSum(numbers []int, target int) []int {
    var last_i int
    for i:=0;i<len(numbers)-1;i++ {
        if i!=0 && last_i == numbers[i] {
            continue
        }
        for j:=i+1;j<len(numbers);j++ {
            if numbers[i] + numbers[j] == target {
                return []int{i+1, j+1}
            }
            if numbers[i] + numbers[j] > target {
                break
            }
        }
        last_i = numbers[i]
    }
    return []int{}
}

Comments