add formatting and example outputs for quick debugging

This commit is contained in:
Vitor Oliveira 2021-09-03 12:33:56 -07:00 committed by GitHub
parent bb967fba48
commit bd92198fc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,9 +18,6 @@
# 0 <= nums.length <= 3000
#-105 <= nums[i] <= 105
# Two Pointer Approach - O(n) Time / O(1) Space
# Return edge cases.
# Sort nums, and init ans array
@ -35,7 +32,6 @@
# .. (left += 1 while nums[left] == nums[left - 1] && left < right)
# Return ans[]
# @param {Integer[]} nums
# @return {Integer[][]}
def three_sum(nums)
@ -43,7 +39,8 @@ def three_sum(nums)
return [] if nums.length < 3
# sort nums, init ans array
nums, ans = nums.sort, []
nums = nums.sort
ans = []
# loop through nums
nums.each_with_index do |val, ind|
@ -51,7 +48,8 @@ def three_sum(nums)
next if ind > 0 && nums[ind] == nums[ind - 1]
# init & run two pointer search
left, right = ind + 1, nums.length - 1
left = ind + 1
right = nums.length - 1
while left < right
# find current sum
@ -68,9 +66,7 @@ def three_sum(nums)
left += 1
while nums[left] == nums[left - 1] && left < right
left += 1
end
left += 1 while nums[left] == nums[left - 1] && left < right
end
end
end
@ -78,3 +74,15 @@ def three_sum(nums)
# return answer
ans
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: []