LeetCode 015 3Sum

【题目】

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

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)

【题意】

给定一个数组,找出和为0的左右三数组合

两点注意:1. 不能有重复组合

2. 组合中的三个数要升序排列

【思路】

1. 对数组现升序排序

2. 维护三个指针p1,p2,p3分别用来确定组合中的第1,2,3个数

首先用p1指针从数组首元素开始依次确定第1个数。为了保证组合的唯一性,p1不能重复指向相同的数

在确定p1后,令p2=p1+1, p3=num.size()-1,这个时候就变成了"two sum"问题。

【注意,这里在做two sum的过程中需要注意p2的去重】

【代码】

class Solution {
public:
    vector<vector<int> > threeSum(vector<int> &num) {
        vector<vector<int> >result;
        int size=num.size();
        if(size<3)return result;                //判断数组长度是否大于3
        sort(num.begin(), num.end());           //排序
        for(int p1=0; p1<size-2; p1++){
            if(p1!=0 && num[p1]==num[p1-1])continue;    //第一位排重
            int p2=p1+1;
            int p3=size-1;
            while(p2<p3){
                if(p2!=p1+1 && num[p2]==num[p2-1]){p2++; continue;} //第二位排重
                int sum=num[p1]+num[p2]+num[p3];
                if(sum==0){
                    vector<int> triplet;
                    triplet.push_back(num[p1]);
                    triplet.push_back(num[p2]);
                    triplet.push_back(num[p3]);
                    result.push_back(triplet);
                    p2++;p3--;
                }
                else if(sum>0)p3--;
                else p2++;
            }
        }
        return result;
    }
};

LeetCode 015 3Sum,布布扣,bubuko.com

时间: 2024-08-07 08:26:16

LeetCode 015 3Sum的相关文章

[LeetCode] 015. 3Sum (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 015.3Sum (Medium) 链接: 题目:https://oj.leetcode.com/problems/3sum/ 代码(github):https://github.com/illuz/leetcode 题意: 在给定数列中找出三个数,使和为 0. 分析: 先排序,再左右夹逼,复杂度 O(n*n).

[Leetcode][015] 3Sum (Java)

题目在这里: https://leetcode.com/problems/3sum/ [标签] Array; Two Pointers [个人分析] 老实交待,这个题卡半天,第一次做不会,抄别人的.过了很久,第二次做,还是不会…….好几次都是Time Limited Error.在看过正确答案之后,才知道是用的Two Pointers + sort 做的优化. 怎么优化? 简单说,就是通过 排序 + 跳过重复(利用Two Pointers) 来达到题目中避免 duplicates的要求. 核心思

Java for LeetCode 015 3Sum

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solut

[Leetcode][016] 3Sum Closest (Java)

题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum 1 public class Solution { 2 public int threeSumClosest(int[] nums, int target) { 3 int result = target; 4 int

[LeetCode] 016. 3Sum Closest (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 016.3Sum_Closest (Medium) 链接: 题目:https://oj.leetcode.com/problems/3sum-closest/ 代码(github):https://github.com/illuz/leetcode 题意: 在给定数列中找出三个数,使和最接近 target. 分析:

LeetCode 016 3Sum Closest

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

No.015 3Sum

15. 3Sum Total Accepted: 131800 Total Submissions: 675028 Difficulty: Medium Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solutio

[C++]LeetCode: 70 3Sum

题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s

【LeetCode】015 3Sum

题目: Given an array S of n integers, are there elements a, b, c in S 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. For example, given array S = [