Short ruby code cost O(n) time


  • 0
    O
    def two_sum(nums, target)
    	map = {}
    	nums.each.with_index {|x, j| map[target - x] = j} 
    	nums.each.with_index {|x, j| return [map[x], j] if map[x] and j != map[x]}
    end
    

Log in to reply
 

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