Skip to content

Commit 724b9d5

Browse files
committed
3sum
1 parent 12998d5 commit 724b9d5

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

3sum/jun0811.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
6+
// 중복 제거 중점..
7+
var threeSum = function (nums) {
8+
nums.sort((a, b) => a - b);
9+
const result = [];
10+
11+
for (let i = 0; i < nums.length - 2; i++) {
12+
if (i > 0 && nums[i] === nums[i - 1]) continue;
13+
14+
let left = i + 1;
15+
let right = nums.length - 1;
16+
17+
while (left < right) {
18+
const sum = nums[i] + nums[left] + nums[right];
19+
20+
if (sum === 0) {
21+
result.push([nums[i], nums[left], nums[right]]);
22+
23+
// 중복 미리 제거
24+
while (left < right && nums[left] === nums[left + 1]) left++;
25+
while (left < right && nums[right] === nums[right - 1]) right--;
26+
27+
left++;
28+
right--;
29+
} else if (sum < 0) {
30+
left++;
31+
} else {
32+
right--;
33+
}
34+
}
35+
}
36+
37+
return result;
38+
};

0 commit comments

Comments
 (0)