My Ruby Solution


  • 0
    T

    Time Complexity - O(n)
    Space Complexity - O(n)

    • n = length of nums
    def two_sum(nums, target)
      seen = {}
    
      nums.each_with_index do |num, idx|
        other_idx = seen[target - num]
    
        if other_idx && other_idx != idx
          return [other_idx, idx]
        end
    
        seen[num] = idx
      end
    end
    

Log in to reply
 

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