Golang Leetcode 167. Two Sum II - Input array is sorted.go

2019-04-12 11:22:36 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412903

思路

从两端向中间遍历

code

代码语言:javascript复制
func twoSum(numbers []int, target int) []int {
	l, r := 0, len(numbers)-1
	for l < r {
		if numbers[l] numbers[r] < target {
			l  
		} else if numbers[l] numbers[r] > target {
			r--
		} else {
			return []int{l   1, r   1}
		}
	}
	return nil
}

0 人点赞