leetcode 3Sum 3Sum Closest 4Sum

这几个题很典型也是国外一些知名公司经常会问到的题

3Sum:

排序,避免重复,时间复杂度O(n^2)

class Solution {
public:

    vector<vector<int> > threeSum(vector<int> &num) {
        int len=num.size();
        sort(num.begin(),num.begin()+len);
        vector<vector<int> > ret;
        ret.clear();
        if(len<3)
            return ret;

        for(int i=0;i<len;i++)
        {
            if(i>0&&num[i]==num[i-1])
                continue;
            int j=i+1,k=len-1;
            while(j<k)
            {
                if(j>i+1&&num[j]==num[j-1])
                {
                    j++;
                    continue;
                }
                if(k<len-1&&num[k]==num[k+1])
                {
                    k--;
                    continue;
                }

                int sum=num[i]+num[j]+num[k];
                if(sum>0)
                {
                    k--;
                }
                else if(sum<0)
                {
                    j++;
                }
                else
                {
                    vector<int> tmp;
                    tmp.push_back(num[i]);
                    tmp.push_back(num[j]);
                    tmp.push_back(num[k]);
                    ret.push_back(tmp);
                    j++;
                }

            }
        }
        return ret;
    }
};

3Sum Closest :

class Solution {

public:

int threeSumClosest(vector<int> &num, int target) {

int len =num.size();

int ret;

sort(num.begin(),num.end());

int i,j,k;

int first=true;

for(int i=0;i<len;i++)

{

j=i+1;

k=len-1;

while(j<k)

{

int sum=num[i]+num[j]+num[k];

if(first)

{

ret=sum;

first=false;

}

else

{

if(abs(ret-target)>abs(sum-target))

ret=sum;

}

if(ret==target)

return ret;

if(sum<target)

j++;

else

k--;

}

}

return ret;

}

};

4Sum:在3sum的基础上再添加一维,时间复杂度变为O(n^3)

class Solution {

public:

vector<vector<int> > fourSum(vector<int> &num, int target) {

int len=num.size();

vector<vector<int> > ret;

sort(num.begin(),num.end());

for(int i=0;i<len;i++)

{

if(i!=0&&num[i]==num[i-1])

continue;

for(int j=i+1;j<len;j++)

{

if(j>i+1 && num[j]==num[j-1])

{

continue;

}

int a=j+1;

int b=len-1;

while(a<b)

{

if(a!=j+1 && num[a]==num[a-1])

{

a++;

continue;

}

if(b!=len-1 && num[b]==num[b+1])

{

b--;

continue;

}

int sum=num[i]+num[j]+num[a]+num[b];

if(sum==target)

{

vector<int> temp;

temp.push_back(num[i]);

temp.push_back(num[j]);

temp.push_back(num[a]);

temp.push_back(num[b]);

ret.push_back(temp);

a++;

b--;

}

else if(sum>target)

{

b--;

}

else

{

a++;

}

}

}

}

return ret;

}

};

leetcode 3Sum 3Sum Closest 4Sum,布布扣,bubuko.com

时间: 2024-12-05 11:36:18

leetcode 3Sum 3Sum Closest 4Sum的相关文章

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 = {

[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

[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: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The

[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][JavaScript]3Sum Closest

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 arr

LeetCode #16 3Sum Closest (M)

[Problem] 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

Java [leetcode 16] 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 =

LeetCode 16 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 = {-1 2