1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// @Title: 两数之和 II - 输入有序数组 (Two Sum II - Input array is sorted)
// @Author: 15816537946@163.com
// @Date: 2019-11-15 15:31:59
// @Runtime: 4 ms
// @Memory: 3 MB
func twoSum(numbers []int, target int) []int {
slow,fast := 0, len(numbers)-1
for slow<fast {
if numbers[slow] +numbers[fast] == target {
return []int{slow+1,fast+1}
} else if numbers[slow] +numbers[fast] > target {
fast--
} else {
slow++
}
}
return nil
}
|