15. 3Sum 和 18. 4Sum

来源:互联网 发布:三星ssd数据迁移 编辑:程序博客网 时间:2024/05/02 13:37

3sum

Given an array S of n integers, are 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:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
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)

主要思想和2sum一样,固定第一个数,然后用2sum同样的方法去找另外两个数。
需要注意的是,为了去掉重复的,需要跳过已经用过的数字

class Solution {public:    vector<vector<int>> threeSum(vector<int>& nums) {        vector<vector<int>> result;        if(nums.size() < 3) return result;        sort(nums.begin(), nums.end());        for(int i = 0; i < nums.size();i++){            int target = -nums[i];            int l = i+1,r = nums.size()-1;            while(l < r) {                int sum = nums[l] + nums[r];                if( sum < target)                    l++;                else if(sum > target)                    r--;                else{                    vector<int> res(3);                    res[0] = nums[i];                    res[1] = nums[l];                    res[2] = nums[r];                    result.push_back(res);                    while(l < r && nums[l] == res[1]) l++;  //用过的数字跳过                    while(l < r && nums[r] == res[2]) r--;                 }             }            while(i+1< nums.size() && nums[i] == nums[i+1]) i++; //用过的数字跳过        }        return result;    }};

4sum

Given an array S of n integers, are 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:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
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) {        vector<vector<int>> result;        if(nums.size() <4) return result;        sort(nums.begin(), nums.end());        for(int i = 0; i < nums.size();i++){            int target1 = target - nums[i];            for(int j = i+1;j < nums.size();j++){                int target2 = target1 - nums[j];                int l = j+1, r= nums.size()-1;                while(l < r){                    int sum = nums[l] + nums[r];                    if(sum < target2)                        l++;                    else if (sum > target2)                        r--;                    else {                        vector<int> res(4);                        res[0] = nums[i];                        res[1] = nums[j];                        res[2] = nums[l];                        res[3] = nums[r];                        result.push_back(res);                        while(l < r && nums[l] == res[2]) l++;                        while(l < r && nums[r] == res[3]) r--;                    }                }                while(j+1 < nums.size() && nums[j] == nums[j+1]) j++;            }            while(i+1 < nums.size() && nums[i] == nums[i+1]) i++;        }        return result;    }};
0 0
原创粉丝点击