-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path015.rb
More file actions
36 lines (33 loc) · 678 Bytes
/
015.rb
File metadata and controls
36 lines (33 loc) · 678 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
# @param {Integer[]} nums
# @return {Integer[][]}
def three_sum(nums)
nums.sort!
results = []
i = 0
while i < nums.length do
j = i + 1
k = nums.length - 1
break if j >= k
target = -nums[i]
break if target < 0
while j < k do
current_target = nums[j] + nums[k]
if current_target == target
results.push([nums[i], nums[j], nums[k]])
while nums[j + 1] == nums[j] && j < k do
j += 1
end
j += 1
elsif current_target > target
k -= 1
else
j += 1
end
end
while nums[i + 1] == nums[i] && i < nums.length do
i += 1
end
i += 1
end
results
end