Advertisement
Alexxik

Untitled

Sep 13th, 2023 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.39 KB | None | 0 0
  1. // MARK: - TWO SUMM
  2.  
  3. func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
  4.     var dict = [Int:Int]()
  5.     for (index, num) in nums.enumerated() {
  6.         let dif = target - num
  7.        
  8.         if dict[dif] != nil {
  9.             return [dict[dif]!, index]
  10.         }
  11.  
  12.         if dict[dif] == nil {
  13.             dict[num] = index
  14.         }
  15.     }
  16.     return []
  17. }
  18.  
  19. //twoSum([2,7,11,15], 9)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement