Ruby Solution


  • 0
    A
    def two_sum(nums, target)
        @return = []
        @hash   = Hash.new
        nums.each_with_index do |num, index|
            difference = target - nums[index]
            if !@hash[difference].nil?
                @return = [@hash[difference], index]
            end
            @hash[num] = index
            break if !@return.empty?
        end
        @return
    end
    

    Is there a better way to do this?


Log in to reply
 

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