-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreeSum.jl
41 lines (33 loc) · 1.04 KB
/
threeSum.jl
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
function threeSum(nums::Vector{Int})::Vector{Vector{Int}}
res = Vector{Vector{Int}}()
sort!(nums) # sort the array in order to apply two pointers
# check early termination
if length(nums) < 3
return res
end
for (i, a) in enumerate(nums)
if a > 0
break # the remaining elements are all positive, can no longer add up to 0
end
if i > 1 && a == nums[i - 1]
continue # skip duplicates
end
l , r = i + 1, length(nums) # Julia's index starts from 1, so its length instead of length - 1
while l < r
threesum = a + nums[l] + nums[r]
if threesum > 0
r -= 1
elseif threesum < 0
l += 1
else
push!(res,[a, nums[l], nums[r]])
l += 1
r -= 1
while nums[l] == nums[l - 1] && l < r # julia uses && instead of and
l += 1
end
end
end
end
return res
end