Accepted Swift One-Pass Hash Table O(n)


  • 0
    N
    class Solution {
        func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
            var hashTable = [Int: Int]()
            for i in 0..<nums.count {
                let complement = target - nums[i]
                if let index = hashTable[complement], index != i {
                    return [index, i]
                }
                hashTable[nums[i]] = i
            }
            return [Int]()
        }
    }
    

Log in to reply
 

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