1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// @Title: 和为s的两个数字 (和为s的两个数字 LCOF)
// @Author: 15816537946@163.com
// @Date: 2022-02-13 22:10:22
// @Runtime: 140 ms
// @Memory: 8.4 MB
func twoSum(nums []int, target int) []int {
    lo, hi := 0, len(nums)-1

    for lo < hi {
        s := nums[lo] + nums[hi]
        if s > target {
            hi--
        } else if s < target {
            lo++
        } else {
            return []int{nums[lo],nums[hi]}
        }
    }

    return nil
}