two-sum-ii-input-array-is-sorted

This commit is contained in:
Gleb Koval 2021-11-07 20:18:20 +00:00
parent 68da3956a9
commit 211d8c6366
No known key found for this signature in database
GPG Key ID: DF27F6A77C48FDA0
1 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,18 @@
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{}
}