Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MARK: - 167. Two Sum II - Input Array Is Sorted
- func twoSum(_ numbers: [Int], _ target: Int) -> [Int] {
- var l = 0
- var r = numbers.count - 1
- while l < r {
- let sum = numbers[l] + numbers[r]
- if sum < target {
- l += 1
- } else if sum > target {
- r -= 1
- } else {
- return [l+1, r+1]
- }
- }
- return []
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement