Swift solution 36ms


  • 4
    A
    class Solution {
        func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
            
            var hash: [Int : Int] = [:]
            var resArray : [Int] = [];
            
            for (i, j) in nums.enumerated() {
                if let index = hash[target - j]{
                    resArray.append(index)
                    resArray.append(i)
                    return resArray
                }
                hash[j] = i
                
            }
            return resArray;
        }
    }
    

  • 0
    Z

    @arias amazing


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.