leet-code/two-sum-ii-input-array-is-s.../sol.go

19 lines
294 B
Go
Raw Normal View History

2021-11-07 20:18:20 +00:00
package main
// Time: O(n)
// Space: O(1)
func twoSum(numbers []int, target int) []int {
p1, p2 := 0, len(numbers)-1
for p1 < p2 {
s := numbers[p1] + numbers[p2]
if s > target {
p2--
} else if s < target {
p1++
} else {
return []int{p1 + 1, p2 + 1}
}
}
return []int{}
}