Skip to content

Commit bd92198

Browse files
authored
add formatting and example outputs for quick debugging
1 parent bb967fb commit bd92198

File tree

1 file changed

+75
-67
lines changed

1 file changed

+75
-67
lines changed

data_structures/arrays/3sum.rb

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,88 @@
1-
#Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] ..
2-
#.. such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
3-
#Notice that the solution set must not contain duplicate triplets.
1+
# Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] ..
2+
# .. such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
3+
# Notice that the solution set must not contain duplicate triplets.
44

5-
#Example 1:
6-
#Input: nums = [-1,0,1,2,-1,-4]
7-
#Output: [[-1,-1,2],[-1,0,1]]
5+
# Example 1:
6+
# Input: nums = [-1,0,1,2,-1,-4]
7+
# Output: [[-1,-1,2],[-1,0,1]]
88

9-
#Example 2:
10-
#Input: nums = []
11-
#Output: []
9+
# Example 2:
10+
# Input: nums = []
11+
# Output: []
1212

13-
#Example 3:
14-
#Input: nums = [0]
15-
#Output: []
13+
# Example 3:
14+
# Input: nums = [0]
15+
# Output: []
1616

17-
#Constraints:
18-
#0 <= nums.length <= 3000
17+
# Constraints:
18+
# 0 <= nums.length <= 3000
1919
#-105 <= nums[i] <= 105
2020

21+
# Two Pointer Approach - O(n) Time / O(1) Space
22+
# Return edge cases.
23+
# Sort nums, and init ans array
24+
# For each |val, index| in nums:
25+
# if the current value is the same as last, then go to next iteration
26+
# init left and right pointers for two pointer search of the two sum in remaining elements of array
27+
# while left < right:
28+
# find current sum
29+
# if sum > 0, right -= 1
30+
# if sum < 0, left += 1
31+
# if it's 0, then add the values to the answer array, and set the left pointer to the next valid value ..
32+
# .. (left += 1 while nums[left] == nums[left - 1] && left < right)
33+
# Return ans[]
2134

35+
# @param {Integer[]} nums
36+
# @return {Integer[][]}
37+
def three_sum(nums)
38+
# return if length too short
39+
return [] if nums.length < 3
2240

41+
# sort nums, init ans array
42+
nums = nums.sort
43+
ans = []
2344

24-
#Two Pointer Approach - O(n) Time / O(1) Space
25-
#Return edge cases.
26-
#Sort nums, and init ans array
27-
#For each |val, index| in nums:
28-
#if the current value is the same as last, then go to next iteration
29-
#init left and right pointers for two pointer search of the two sum in remaining elements of array
30-
#while left < right:
31-
#find current sum
32-
#if sum > 0, right -= 1
33-
#if sum < 0, left += 1
34-
#if it's 0, then add the values to the answer array, and set the left pointer to the next valid value ..
35-
#.. (left += 1 while nums[left] == nums[left - 1] && left < right)
36-
#Return ans[]
45+
# loop through nums
46+
nums.each_with_index do |val, ind|
47+
# if the previous value is the same as current, then skip this iteration as it would create duplicates
48+
next if ind > 0 && nums[ind] == nums[ind - 1]
3749

50+
# init & run two pointer search
51+
left = ind + 1
52+
right = nums.length - 1
3853

39-
# @param {Integer[]} nums
40-
# @return {Integer[][]}
41-
def three_sum(nums)
42-
#return if length too short
43-
return [] if nums.length < 3
44-
45-
#sort nums, init ans array
46-
nums, ans = nums.sort, []
47-
48-
#loop through nums
49-
nums.each_with_index do |val, ind|
50-
#if the previous value is the same as current, then skip this iteration as it would create duplicates
51-
next if ind > 0 && nums[ind] == nums[ind - 1]
52-
53-
#init & run two pointer search
54-
left, right = ind + 1, nums.length - 1
55-
56-
while left < right
57-
#find current sum
58-
sum = val + nums[left] + nums[right]
59-
60-
#decrease sum if it's too great, increase sum if it's too low
61-
if sum > 0
62-
right -= 1
63-
elsif sum < 0
64-
left += 1
65-
#if it's zero, then add the answer to array and set left pointer to next valid value
66-
else
67-
ans << [val, nums[left], nums[right]]
68-
69-
left += 1
70-
71-
while nums[left] == nums[left - 1] && left < right
72-
left += 1
73-
end
74-
end
54+
while left < right
55+
# find current sum
56+
sum = val + nums[left] + nums[right]
57+
58+
# decrease sum if it's too great, increase sum if it's too low
59+
if sum > 0
60+
right -= 1
61+
elsif sum < 0
62+
left += 1
63+
# if it's zero, then add the answer to array and set left pointer to next valid value
64+
else
65+
ans << [val, nums[left], nums[right]]
66+
67+
left += 1
68+
69+
left += 1 while nums[left] == nums[left - 1] && left < right
7570
end
7671
end
77-
78-
#return answer
79-
ans
80-
end
72+
end
73+
74+
# return answer
75+
ans
76+
end
77+
78+
nums = [-1, 0, 1, 2, -1, -4]
79+
print three_sum(nums)
80+
# Output: [[-1,-1,2],[-1,0,1]]
81+
82+
nums = []
83+
print three_sum(nums)
84+
# Output: []
85+
86+
nums = [0]
87+
print three_sum(nums)
88+
# Output: []

0 commit comments

Comments
 (0)