双指针 左右指针 因为数组是升序数组
func twoSum(_ numbers: [Int], _ target: Int) -> [Int] {
var head = 0
var end = numbers.count - 1
while true {
if numbers[head] + numbers[end] > target {
end -= 1
}
else if numbers[head] + numbers[end] < target {
head += 1
}
else if numbers[head] + numbers[end] == target {
return [head,end]
}
}
}
网友评论