leetcode no.15 3sum 感谢您的阅读,请指正出错误

question

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

1
2
3
4
5
6
7
Given array nums = [-1, 0, 1, 2, -1, -4],

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

Solution

method 1

  1. 三重循环暴力解决,排序用来去重(bug)

  2. 使用unique去重

    nums.erase(unique(nums.begin(),nums.end()),nums.end());

    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
    44
    45

    #include<algorithm>
    #include<vector>

    using namespace std;

    static const int accelerate = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return 0;
    }();

    class {
    public:
    vector<vector<int>> threeSum(vector<int>& nums) {
    vector<vector<int>> result;
    int length = nums.size();
    if (length < 3)
    {
    return result;
    }
    sort(nums.begin(), nums.end());
    for (int i = 0; i < length - 2; i++)
    {
    for (int j = i + 1; j < length - 1; j++)
    {
    for (int k = j + 1; k < length; k++)
    {
    if (nums[i] + nums[j] == -nums[k]&&nums[i] !=nums[i-1])
    result.push_back({ nums[i], nums[j], nums[k] });
    }
    }
    }
    return result;
    }
    };

    int main(void)
    {
    vector<int> nums = { -1, 0, 1, 2, -1, -4 };
    vector<vector<int>> result;
    Solution s;
    result=s.threeSum(nums);
    return 0;
    }

method 2

  1. 双指针

  2. 去重通过缩小区间(忽略和使用的元素相等的元素来实现)

    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
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57

    #include<algorithm>
    #include<vector>

    using namespace std;

    static const int accelerate = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return 0;
    }();

    class {
    public:
    vector<vector<int> > threeSum(vector<int>& nums) {
    int length = nums.size();
    if (length <= 2) return {};
    vector<vector<int> > triple;
    sort(nums.begin(), nums.end());

    for (int i = 0; i < length;) {
    int start = i + 1, end = length - 1;

    while (start < end) {
    if (nums[i] + nums[start] + nums[end] == 0) {
    triple.push_back({ nums[i],nums[start],nums[end] });
    start++;
    end--;
    while ((start < end) && nums[start] == nums[start - 1]) start++;
    while ((start < end) && nums[end] == nums[end + 1]) end--;
    }
    else if (nums[i] + nums[start] + nums[end] < 0) {
    start++;
    while ((start < end) && nums[start] == nums[start - 1]) start++;
    }
    else {
    end--;
    while ((start < end) && nums[end] == nums[end + 1]) end--;
    }
    }
    i++;
    while ((i < length) && nums[i] == nums[i - 1])
    i++;

    }
    return triple;
    }
    };

    int main(void)
    {
    vector<int> nums = { -1,0,1,2,-1,-4};
    vector<vector<int>> triple;
    Solution s;
    triple=s.threeSum(nums);
    return 0;
    }

to be continued
2019年4月6日 18点56分


感谢您的阅读,请指正出错误