-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeNumberSum.js
More file actions
59 lines (48 loc) · 1.58 KB
/
threeNumberSum.js
File metadata and controls
59 lines (48 loc) · 1.58 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
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.
**/
O(n^2)
function threeSum(nums)
nums.sort((a, b) => a - b);
const result = [];
for(let i = 0; i < nums.length; i++) {
let low = i+1, high = nums.length-1, sum = 0;
while(low < high) {
sum = nums[i] + nums[low] + nums[high];
if(sum === 0) {
result.push([nums[i], nums[low], nums[high]]);
while(nums[low+1] === nums[low]) low++;
while(nums[high-1] === nums[high]) high--;
low++;
high--;
} else if(sum < 0) low++;
else high--;
}
while(nums[i+1] === nums[i]) i++;
}
return result;
//algo expert solution
function threeNumberSum(array, targetSum) {
// Write your code here.
array.sort((a,b) => a-b);
const result = [];
for (let i = 0; i < array.length - 2; i++) {
let left = i + 1;
let right = array.length - 1;
while(left < right) {
const currentSum = array[i] + array[left] + array[right];
if(currentSum === targetSum) {
result.push([array[i], array[left], array[right]]);
left++;
right--;
} else if(currentSum < targetSum) {
left++;
} else if(currentSum > targetSum) {
right--;
}
}
}
return result;
}
// Do not edit the line below.
exports.threeNumberSum = threeNumberSum;