Advertisement
Alexxik

Untitled

Sep 11th, 2023 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.40 KB | None | 0 0
  1. // MARK: - 167. Two Sum II - Input Array Is Sorted
  2.  
  3. func twoSum(_ numbers: [Int], _ target: Int) -> [Int] {
  4.     var l = 0
  5.     var r = numbers.count - 1
  6.    
  7.     while l < r {
  8.         let sum = numbers[l] + numbers[r]
  9.         if sum < target {
  10.             l += 1
  11.         } else if sum > target {
  12.             r -= 1
  13.         } else {
  14.             return [l+1, r+1]
  15.         }
  16.     }
  17.     return []
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement