Accepted Ruby O(n) Solution


  • 0
    B
    def two_sum(nums, target)
        array = []
        hash = {}
        nums.each_with_index do |num, index|
            if hash.key?(target - num)
                array << hash[target - num]
                array << index
                return array
            else
                hash.merge!({ num => index })
            end
        end
        array
    end
    

Log in to reply
 

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