Accepted Ruby Solution O(n)


  • 0
    S
    require 'set'
    
    def two_sum(nums, target)
      compliments = Hash.new()
      lo_index = nil
      high_index = nil
      nums.each_with_index do |number, index|
          found_index = compliments[number]
          if found_index
            lo_index = found_index
            high_index = index
            break
          end
          compliments[target-number] = index
      end
      (lo_index && high_index) ? [lo_index, high_index] : []
    end
    

Log in to reply
 

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