Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15. 3Sum #209

Open
Tcdian opened this issue Jun 12, 2020 · 1 comment
Open

15. 3Sum #209

Tcdian opened this issue Jun 12, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Jun 12, 2020

15. 3Sum

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。

Example

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

Note

  • 答案中不可以包含重复的三元组。
@Tcdian
Copy link
Owner Author

Tcdian commented Jun 12, 2020

Solution

  • JavaScript Solution
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var threeSum = function(nums) {
    const result = [];
    const sortedNums = [...nums].sort((a, b) => a - b);
    for (let i = 0; i < sortedNums.length - 2; i++) {
        if (sortedNums[i] > 0) {
            break;
        }
        if (i > 0 && sortedNums[i] === sortedNums[i - 1]) {
            continue;
        }
        let left = i + 1;
        let right = sortedNums.length - 1;
        while (left < right) {
            if (left > i + 1 && sortedNums[left] === sortedNums[left - 1]) {
                left++;
                continue;
            }
            if (
                right < sortedNums.length - 1
                && sortedNums[right] === sortedNums[right + 1]
            ) {
                right--;
                continue;
            }
            if (sortedNums[i] + sortedNums[left] + sortedNums[right] < 0) {
                left++;
            } else if (sortedNums[i] + sortedNums[left] + sortedNums[right] > 0) {
                right--;
            } else {
                result.push([sortedNums[i], sortedNums[left], sortedNums[right]]);
                left++;
                right--;
            }
        }
    }
    return result;
};
  • TypeScript Solution
function threeSum(nums: number[]): number[][] {
    const result: number[][] = [];
    const sortedNums = [...nums].sort((a, b) => a - b);
    for (let i = 0; i < sortedNums.length - 2; i++) {
        if (sortedNums[i] > 0) {
            break;
        }
        if (i > 0 && sortedNums[i] === sortedNums[i - 1]) {
            continue;
        }
        let left = i + 1;
        let right = sortedNums.length - 1;
        while (left < right) {
            if (left > i + 1 && sortedNums[left] === sortedNums[left - 1]) {
                left++;
                continue;
            }
            if (
                right < sortedNums.length - 1
                && sortedNums[right] === sortedNums[right + 1]
            ) {
                right--;
                continue;
            }
            if (sortedNums[i] + sortedNums[left] + sortedNums[right] < 0) {
                left++;
            } else if (sortedNums[i] + sortedNums[left] + sortedNums[right] > 0) {
                right--;
            } else {
                result.push([sortedNums[i], sortedNums[left], sortedNums[right]]);
                left++;
                right--;
            }
        }
    }
    return result;
}

@Tcdian Tcdian added the Classic label Jun 12, 2020
@Tcdian Tcdian removed the Classic label Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant