Swift O(n) validated solution


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

Log in to reply
 

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