-
Notifications
You must be signed in to change notification settings - Fork 374
/
two_sum_ii_test.go
76 lines (64 loc) · 1.68 KB
/
two_sum_ii_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Problem:
- Given a sorted array of integers, return indices of the two numbers such
that they add up to a specific target.
Example:
- Input: nums = []int{2, 3, 4}, target = 6
Output: [0, 2] since nums[0] + nums[2] = 2 + 4 = 6
Approach
- Since the array is sorted, can use two-pointer approach that has one point
to the start of the list while the other point at the end and move the
toward each other.
Solution:
- Have one pointer point to the start of the list and the other point at the end.
- Iterate through the list and check their sum.
- If it is smaller than the target, increment the start pointer to make the
sum bigger little by little.
- Otherwise, decrement the end pointer to make the sum smaller.
- When the sum is equal to the target, we found the answer.
Cost:
- O(n) time, O(1) space.
*/
package leetcode
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestTwoSumII(t *testing.T) {
tests := []struct {
in1 []int
in2 int
expected []int
}{
{[]int{}, 6, []int{0, 0}},
{[]int{1}, 6, []int{0, 0}},
{[]int{2, 4}, 6, []int{0, 1}},
{[]int{2, 5}, 6, []int{0, 0}},
{[]int{2, 3, 4}, 6, []int{0, 2}},
{[]int{2, 5, 8}, 6, []int{0, 0}},
{[]int{2, 3, 4, 10}, 6, []int{0, 2}},
}
for _, tt := range tests {
result := twoSumII(tt.in1, tt.in2)
common.Equal(t, tt.expected, result)
}
}
func twoSumII(nums []int, target int) []int {
// start and end points to the start and end index of the list.
start := 0
end := len(nums) - 1
out := make([]int, 2)
for start < end {
sum := nums[start] + nums[end]
if sum < target {
start++
} else if sum > target {
end--
} else {
out[0] = start
out[1] = end
break
}
}
return out
}