Leetcode-Sum, 3Sum, 3Sum Closest, 4Sum

Source: Internet
Author: User
Tags abs

Given an array of integers, return indices of the both numbers such that they add-to a specific target.

You may assume this each input would has exactly one solution, and you could not use the same element twice.

Example:

Given nums = [2, 7, one, +], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Class Solution {public
:
    vector<int> twosum (vector<int>& nums, int target) {
        map<int, int>mp;
        vector<int>res;
        int len = Nums.size ();
        for (int i=0;i<len;i++)
            mp[nums[i]]=i;
        for (int i=0;i<len;i++)
        {
            if (Mp[target-nums[i]])
            {
                res.push_back (i);
                Res.push_back (Mp[target-nums[i]);
                return res;}}}
;

Given an array S of n integers, is 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 = [-1, 0, 1, 2,-1,-4], A solution set is: [[-1, 0, 1], [-1,-1, 2]] 
Class Solution {public:vector<vector<int>> threesum (vector<int>& nums) {int len=nums.
        Size ();
        vector<vector<int> > Res;
        if (len<=2) return res;
        Sort (Nums.begin (), Nums.end ());
        for (int i=0;i<len;)
            {int st=i+1,ed=len-1; while (st<ed) {if (nums[i]+nums[st]+nums[ed]==0) {RES.P
                    Ush_back ({nums[i],nums[st],nums[ed]});
                    st++;
                    ed--;
                    while (st<ed&&nums[st]==nums[st-1]) st++;
                while (st<ed&&nums[ed]==nums[ed+1]) ed--;
                    } else if (nums[i]+nums[st]+nums[ed]<0) {st++;
                while (st<ed&&nums[st]==nums[st-1]) st++;
} else if (nums[i]+nums[st]+nums[ed]>0) {ed--;                    while (st<ed&&nums[ed]==nums[ed+1]) ed--;
            }} i++;
        while ((I<len) && (nums[i]==nums[i-1)) i++;
    } return res; }
};

Given an array S of n integers, find three integers in S such, the sum was closest to a Given number, target. Return the sum of the three integers. You may assume this each input would has exactly one solution.

    For example, given array S = {-1 2 1-4}, and target = 1.

    The sum is closest to the target is 2. (-1 + 2 + 1 = 2).
class solution {public:int threesumclosest (vector<int>& nums, int target) {int L
        En=nums.size ();
        Sort (Nums.begin (), Nums.end ());
        int res=nums[0]+nums[1]+nums[2];
        if (len==3) return res;
            for (int i=0;i<len-2;i++) {int st=i+1,ed=len-1;
                while (st<ed) {int sum=nums[i]+nums[st]+nums[ed];
                    if (ABS (Sum-target) <abs (res-target)) {res=sum;
                if (res==target) return res;
                } if (Sum>target) ed--;
            else st++;
    }} return res; }
};

Given an array S of n integers, is there elements a, B, C, and D in S such that A + B + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:the solution set must not contain duplicate quadruplets.

For example, given array S = [1, 0,-1, 0,-2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2,-1, 1, 2],
  [-2,  0, 0, 2]
]
Class Solution {public:vector<vector<int>> foursum (vector<int>& nums, int target) {ve
        ctor<vector<int>> Res;
        int len=nums.size ();
        if (len<4) return res;
        Sort (Nums.begin (), Nums.end ());
            for (int i=0;i<len-3;i++) {if (i>0&&nums[i]==nums[i-1]) continue;
            if (nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;
            if (nums[i]+nums[len-1]+nums[len-2]+nums[len-3]<target) continue;
                for (int j=i+1;j<len-2;j++) {if (j>i+1&&nums[j]==nums[j-1]) continue;
                if (nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break;
                if (nums[i]+nums[j]+nums[len-1]+nums[len-2]<target) continue;
                int st=j+1,ed=len-1;
                    while (st<ed) {int sum=nums[i]+nums[j]+nums[st]+nums[ed];
if (sum==target) {                        Res.push_back ({nums[i],nums[j],nums[st],nums[ed]});
                        ed--;
                        while (st<ed&&nums[ed]==nums[ed+1]) ed--;
                        st++;
                    while (st<ed&&nums[st]==nums[st-1]) st++;
                        } else if (sum>target) {ed--;
                    while (st<ed&&nums[ed]==nums[ed+1]) ed--;
                        } else if (sum<target) {st++;
                    while (st<ed&&nums[st]==nums[st-1]) st++;
    }}}} return res; }
};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.