forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhozzijeong.js
More file actions
45 lines (32 loc) · 998 Bytes
/
hozzijeong.js
File metadata and controls
45 lines (32 loc) · 998 Bytes
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
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
nums.sort((a,b) => a-b);
const length = nums.length;
const answer = [];
for(let i = 0; i < length - 2; i++){
if(i > 0 && nums[i] === nums[i-1]) continue;
if(nums[i] > 0) break;
let left = i + 1;
let right = length -1;
while(left < right){
const result = nums[left]+ nums[right] + nums[i];
if(result > 0) {
right -= 1;
}
if(result < 0) {
left +=1;
}
if(result === 0) {
answer.push([nums[i],nums[left],nums[right]]);
while(left < right && nums[left] === nums[left + 1]) left += 1;
while(left < right && nums[right] === nums[right - 1]) right -=1;
left += 1;
right -= 1;
}
}
}
return answer
};