Accepted Swift O(n) Solution


  • 0
    B
    class Solution {
        func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
            var dict = [Int: Int]()
            for (index, element) in nums.enumerated() {
                dict[element] = index
            }
            
            for (index, element) in nums.enumerated() {
                if let value = dict[target-element], index != value {
                    return [index, value]
                }
            }
            return []
        }
    }
    

Log in to reply
 

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