forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhwanmini.js
More file actions
39 lines (33 loc) · 1.11 KB
/
hwanmini.js
File metadata and controls
39 lines (33 loc) · 1.11 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
// 시간복잡도 O(n2)
// 공간복잡도 O(k) (k는 결과 개수)
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
nums.sort((a, b) => a - b);
let result = []
for (let i = 0 ; i <= nums.length - 2; i++) {
if (i > 0 && nums[i] === nums[i-1]) continue
const curNum = nums[i]
let leftIdx = i+1;
let rightIdx = nums.length - 1;
while (leftIdx < rightIdx) {
const leftNum = nums[leftIdx]
const rightNum = nums[rightIdx]
const threeSum = curNum + leftNum + rightNum
if (threeSum === 0) {
result.push([curNum, leftNum, rightNum])
while (leftIdx < rightIdx && nums[leftIdx] === nums[leftIdx+1]) leftIdx++
while (leftIdx < rightIdx && nums[rightIdx] === nums[rightIdx-1]) rightIdx--
leftIdx++
rightIdx--
} else if (threeSum < 0) {
leftIdx = leftIdx + 1
} else if (threeSum > 0) {
rightIdx = rightIdx - 1
}
}
}
return result
};