mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-14 08:01:05 +01:00
add formatting and example outputs for quick debugging
This commit is contained in:
parent
bb967fba48
commit
bd92198fc8
1 changed files with 76 additions and 68 deletions
|
@ -1,80 +1,88 @@
|
||||||
#Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] ..
|
# Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] ..
|
||||||
#.. such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
|
# .. such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
|
||||||
#Notice that the solution set must not contain duplicate triplets.
|
# Notice that the solution set must not contain duplicate triplets.
|
||||||
|
|
||||||
#Example 1:
|
# Example 1:
|
||||||
#Input: nums = [-1,0,1,2,-1,-4]
|
# Input: nums = [-1,0,1,2,-1,-4]
|
||||||
#Output: [[-1,-1,2],[-1,0,1]]
|
# Output: [[-1,-1,2],[-1,0,1]]
|
||||||
|
|
||||||
#Example 2:
|
# Example 2:
|
||||||
#Input: nums = []
|
# Input: nums = []
|
||||||
#Output: []
|
# Output: []
|
||||||
|
|
||||||
#Example 3:
|
# Example 3:
|
||||||
#Input: nums = [0]
|
# Input: nums = [0]
|
||||||
#Output: []
|
# Output: []
|
||||||
|
|
||||||
#Constraints:
|
# Constraints:
|
||||||
#0 <= nums.length <= 3000
|
# 0 <= nums.length <= 3000
|
||||||
#-105 <= nums[i] <= 105
|
#-105 <= nums[i] <= 105
|
||||||
|
|
||||||
|
# Two Pointer Approach - O(n) Time / O(1) Space
|
||||||
|
# Return edge cases.
|
||||||
|
# Sort nums, and init ans array
|
||||||
#Two Pointer Approach - O(n) Time / O(1) Space
|
# For each |val, index| in nums:
|
||||||
#Return edge cases.
|
# if the current value is the same as last, then go to next iteration
|
||||||
#Sort nums, and init ans array
|
# init left and right pointers for two pointer search of the two sum in remaining elements of array
|
||||||
#For each |val, index| in nums:
|
# while left < right:
|
||||||
#if the current value is the same as last, then go to next iteration
|
# find current sum
|
||||||
#init left and right pointers for two pointer search of the two sum in remaining elements of array
|
# if sum > 0, right -= 1
|
||||||
#while left < right:
|
# if sum < 0, left += 1
|
||||||
#find current sum
|
# if it's 0, then add the values to the answer array, and set the left pointer to the next valid value ..
|
||||||
#if sum > 0, right -= 1
|
# .. (left += 1 while nums[left] == nums[left - 1] && left < right)
|
||||||
#if sum < 0, left += 1
|
# Return ans[]
|
||||||
#if it's 0, then add the values to the answer array, and set the left pointer to the next valid value ..
|
|
||||||
#.. (left += 1 while nums[left] == nums[left - 1] && left < right)
|
|
||||||
#Return ans[]
|
|
||||||
|
|
||||||
|
|
||||||
# @param {Integer[]} nums
|
# @param {Integer[]} nums
|
||||||
# @return {Integer[][]}
|
# @return {Integer[][]}
|
||||||
def three_sum(nums)
|
def three_sum(nums)
|
||||||
#return if length too short
|
# return if length too short
|
||||||
return [] if nums.length < 3
|
return [] if nums.length < 3
|
||||||
|
|
||||||
#sort nums, init ans array
|
# sort nums, init ans array
|
||||||
nums, ans = nums.sort, []
|
nums = nums.sort
|
||||||
|
ans = []
|
||||||
|
|
||||||
#loop through nums
|
# loop through nums
|
||||||
nums.each_with_index do |val, ind|
|
nums.each_with_index do |val, ind|
|
||||||
#if the previous value is the same as current, then skip this iteration as it would create duplicates
|
# if the previous value is the same as current, then skip this iteration as it would create duplicates
|
||||||
next if ind > 0 && nums[ind] == nums[ind - 1]
|
next if ind > 0 && nums[ind] == nums[ind - 1]
|
||||||
|
|
||||||
#init & run two pointer search
|
# init & run two pointer search
|
||||||
left, right = ind + 1, nums.length - 1
|
left = ind + 1
|
||||||
|
right = nums.length - 1
|
||||||
|
|
||||||
while left < right
|
while left < right
|
||||||
#find current sum
|
# find current sum
|
||||||
sum = val + nums[left] + nums[right]
|
sum = val + nums[left] + nums[right]
|
||||||
|
|
||||||
#decrease sum if it's too great, increase sum if it's too low
|
# decrease sum if it's too great, increase sum if it's too low
|
||||||
if sum > 0
|
if sum > 0
|
||||||
right -= 1
|
right -= 1
|
||||||
elsif sum < 0
|
elsif sum < 0
|
||||||
left += 1
|
left += 1
|
||||||
#if it's zero, then add the answer to array and set left pointer to next valid value
|
# if it's zero, then add the answer to array and set left pointer to next valid value
|
||||||
else
|
else
|
||||||
ans << [val, nums[left], nums[right]]
|
ans << [val, nums[left], nums[right]]
|
||||||
|
|
||||||
left += 1
|
left += 1
|
||||||
|
|
||||||
while nums[left] == nums[left - 1] && left < right
|
left += 1 while nums[left] == nums[left - 1] && left < right
|
||||||
left += 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
#return answer
|
# return answer
|
||||||
ans
|
ans
|
||||||
end
|
end
|
||||||
|
|
||||||
|
nums = [-1, 0, 1, 2, -1, -4]
|
||||||
|
print three_sum(nums)
|
||||||
|
# Output: [[-1,-1,2],[-1,0,1]]
|
||||||
|
|
||||||
|
nums = []
|
||||||
|
print three_sum(nums)
|
||||||
|
# Output: []
|
||||||
|
|
||||||
|
nums = [0]
|
||||||
|
print three_sum(nums)
|
||||||
|
# Output: []
|
||||||
|
|
Loading…
Reference in a new issue