ποΈ Problems
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
two sumμ μ΄μ three sum λ¬Έμ .
β¨ Idea
nums
μμ νλμ μ«μ(value
)λ₯Ό μ ννκ³ , μ€λ³΅μ νΌνκΈ° μν΄μ μ΄ν μ€λ₯Έμͺ½ λλ¨Έμ§ array μμtwoSum
μ μ΄μ©ν΄μ μμ μ νλ-value
λ₯Ό μ°Ύλλ€.twoSum
λ¬Έμ λ sorted arrayμμ two pointers κΈ°λ²μΌλ‘ μ°Ύλλ€.- μ€λ³΅λλ μ«μμ λν μ²λ¦¬κ° μ΄λ €μ λλ°, labuladongμ μ’μ μ± μ μ’μ λ°©λ²μ΄ λμμ μ 리ν΄λ΄ λλ€.
π Intuition
μ«μ νλ ([object Object]) μ ν ν twoSum μΌλ‘ [object Object] κ° μ°ΎκΈ°
- twoSum μ left-right two pointers κΈ°λ² μ¬μ©νκΈ° μνμ¬
nums
μ λ ¬.
nums.sort((a, b) => a - b);
for (let i = 0; i < N; i += 1) {
const cur = nums[i];
twoSum(i + 1, N - 1, -cur);
}
sorted arrayμμ left-right two pointers μ΄μ©ν two sum
Two Sum II - Input Array Is Sorted - LeetCode
nums.sort((a,b) => a - b);
...
const twoSum = (left, right, target) => {
while (left < right) {
const sum = nums[left] + nums[right];
if (sum > target) {
right -= 1;
} else if (sum < target) {
left += 1;
} else if (sum === target) {
return [ left, right ] ;
}
}
}
return [ -1, -1]
π‘ μΌλ°νλ two sum μ left-right two pointers κΈ°λ²
μ€λ³΅λ κ°μ κ²½μ°μλ skip νλλ‘ μ²λ¦¬ μΆκ°ν¨.
leftValue rightValue
| |
... 2 2 ... 3 3
| |
left -> <- right
left
ν¬μΈν°μ κ°μ΄ λ°λλκΉμ§ κ³μ μ¦κ°
while (left < right && nums[left] === leftValue) left += 1;
right
ν¬μΈν°μ κ°μ΄ λ°λλκΉμ§ κ³μ κ°μ
while (left < right && nums[right] === rightValue) right -= 1;
const twoSum = (left, right, target) => {
while (left < right) {
const leftValue = nums[left];
const rightValue = nums[right];
const sum = leftValue + rightValue;
if (sum > target) {
while (left < right && nums[right] === rightValue) right -= 1;
} else if (sum < target) {
while (left < right && nums[left] === leftValue) left += 1;
} else if (sum === target) {
res.push([-target, nums[left], nums[right]]);
while (left < right && nums[right] === rightValue) right -= 1;
while (left < right && nums[left] === leftValue) left += 1;
}
}
};
π‘ μ΅μ΄ κ° μ ν μμλ μ€λ³΅ μ²λ¦¬ μ²λ¦¬ μΆκ°
μμμ μ νν κ°μ΄ λμΌν κ²½μ°μλ skip μ²λ¦¬ μΆκ°
for (let i = 0; i < N; i += 1) {
const cur = nums[i];
twoSum(i + 1, N - 1, -cur);
while (i < N && nums[i] === nums[i + 1]) i += 1;
}
π₯ My Solution
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
const N = nums.length;
nums.sort((a, b) => a - b);
const res = [];
const twoSum = (left, right, target) => {
while (left < right) {
const leftValue = nums[left];
const rightValue = nums[right];
const sum = leftValue + rightValue;
if (sum > target) {
while (left < right && nums[right] === rightValue) right -= 1;
} else if (sum < target) {
while (left < right && nums[left] === leftValue) left += 1;
} else if (sum === target) {
res.push([-target, nums[left], nums[right]]);
while (left < right && nums[right] === rightValue) right -= 1;
while (left < right && nums[left] === leftValue) left += 1;
}
}
};
for (let i = 0; i < N; i += 1) {
const cur = nums[i];
twoSum(i + 1, N - 1, -cur);
while (i < N && nums[i] === nums[i + 1]) i += 1;
}
return res;
};