forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15 3Sum.js
63 lines (48 loc) · 1.7 KB
/
15 3Sum.js
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
60
61
62
63
// Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
// Note: The solution set must not contain duplicate triplets.
// For example, given array S = [-1, 0, 1, 2, -1, -4],
// A solution set is:
// [
// [-1, 0, 1],
// [-1, -1, 2]
// ]
// Hide Company Tags Amazon Microsoft Bloomberg Facebook Adobe
// Hide Tags Array Two Pointers
// Hide Similar Problems (E) Two Sum (M) 3Sum Closest (M) 4Sum (M) 3Sum Smaller
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
var result = [];
if(nums.length < 3){
return result;
}
nums.sort(function(a,b){return a>b ? 1 : -1;});
var len = nums.length;
for(var i = 0; i < len-2; i++){
if(i === 0 || nums[i] > nums[i-1]){ // very important, same as line 40, remove duplicate as 111 will only run once 1-> rather tan 1 1 1
target = 0 - nums[i];
j = i + 1;
k = len - 1;
while(j < k){
if(nums[j] + nums[k] === target){
result.push([nums[i],nums[j],nums[k]]);
j++;
k--;
while(j < k && nums[j] === nums[j-1]){j++;}
while(j < k && nums[k] === nums[k+1]){k--;}
} else if(nums[j] + nums[k] < target){
j++;
} else {
k--;
}
}
}
// very important, same as line 19
// if(i < len - 1){
// while(nums[i] === nums[i+1]){i++;}
// }
}
return result;
};