-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.rb
More file actions
60 lines (36 loc) · 934 Bytes
/
Solution.rb
File metadata and controls
60 lines (36 loc) · 934 Bytes
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
58
59
60
# @param {Integer[]} nums
# @return {Integer[][]}
def three_sum(nums)
nums.sort!
bound = nums.size
result_set = Array.new
index = 0
until index >= bound
left = index + 1; right = bound - 1
until left >= right
if nums[index] + nums[left] + nums[right] == 0
result_set << [nums[index], nums[left], nums[right]]
while left < right and nums[left] == nums[left + 1] do
left += 1
end
while left < right and nums[right] == nums[right - 1] do
right -= 1
end
left += 1
right -= 1
elsif nums[index] + nums[left] + nums[right] < 0
left += 1
else
right -= 1
end
end
index += 1
while index > 0 and nums[index] == nums[index - 1]
index += 1
end
end
result_set
end
# result = three_sum [1, 3, -1, 7, 2, 6, 0, 5]
result = three_sum [-1, 0, 1, 2, -1, -4]
print result