forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdy8739.js
More file actions
45 lines (33 loc) · 992 Bytes
/
jdy8739.js
File metadata and controls
45 lines (33 loc) · 992 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
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
const answer = [];
const sorted = nums.sort((a, b) => a - b);
for (let i=0; i<sorted.length; i++) {
if (i > 0 && sorted[i] === sorted[i - 1]) {
// 위 조건으로 중복 숫자 필터
continue;
}
let l = i + 1;
let h = sorted.length - 1;
while (l < h) {
const sum = sorted[i] + sorted[l] + sorted[h];
if (sum === 0) {
const arr = [sorted[i], sorted[l], sorted[h]];
answer.push(arr);
// 아래 반복문으로 중복 숫자 필터
while (l < h && sorted[l] === sorted[l + 1]) l++;
while (l < h && sorted[h] === sorted[h - 1]) h--;
h--;
l++;
}
if (sum > 0) h--;
if (sum < 0) l++;
}
}
return answer;
};
// TC: O(n2)
// SC: O(1)