forked from mohitjain/leetcode_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_3sum.rb
More file actions
58 lines (52 loc) · 1.29 KB
/
15_3sum.rb
File metadata and controls
58 lines (52 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Leetcode Problem: https://leetcode.com/problems/3sum/
#Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
#
# Notice that the solution set must not contain duplicate triplets.
#
#
#
# Example 1:
#
# Input: nums = [-1,0,1,2,-1,-4]
# Output: [[-1,-1,2],[-1,0,1]]
#
# Example 2:
#
# Input: nums = []
# Output: []
#
# Example 3:
#
# Input: nums = [0]
# Output: []
#
#
#
# Constraints:
#
# 0 <= nums.length <= 3000
# -105 <= nums[i] <= 105
# ----------------------------------------------------------------------------------------------------------------------
# @param {Integer[]} nums
# @return {Integer[][]}
require 'pry'
def three_sum(nums)
data = {}
nums.each_with_index do |num, index|
data[num] = data.fetch(data[num], []).push index
end
output = {}
nums.each_with_index do |num, index|
target = -num
nums.each_with_index do |num2, index2|
next if index == index2
next if data[target - num2].nil?
data[target - num2].each do |original_index|
next if original_index == index || original_index == index2
output.merge! [num, num2, target - num2].sort => true
end
end
end
output.keys
end
p three_sum [-1, 0, 1, 2, -1, -4]