LeetCode 18. 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: 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]
]

题目解析

思路一:
先利用map特性排序,以第i个为头,从右边找是否存在3sum。在3sum中同样使用2sum。
但是超时了,但是思路应该是对的,时间复杂度为O(n^3)。

思路二:
看了题目的标签有 twopointer,将2sum过程改用twopointer方法。使用两个循环;

% 1. 避免重复
if(i>0&&nums[i]==nums[i-1])continue;

% 2. 在循环开始时进行检查
if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;
if(nums[i]+nums[cnt-3]+nums[cnt-2]+nums[cnt-1]continue;

代码(思路一)

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int> >ans_set;
        vector<int> ans(4,0);
        map<int,int>nums_map;
        for(int it:nums)
        {
            nums_map[it]++;
        }
        for(auto it = nums_map.begin();it!=nums_map.end();it++)
        {
            ans[0]=it->first;
            int threesum = target - it->first;
            find_3sum(ans_set,ans,nums_map,threesum);
        }
        return ans_set;
    }
    void find_3sum(vector<vector<int> >&ans_set,vector<int> ans,map<int,int>&nums_map,int threesum)
    {
        nums_map[ans[0]]--;
        for(auto it=nums_map.find(ans[0]);it!=nums_map.end();it++)
        {
            if(it->second<=0)
            {
                continue;
            }
            ans[1]=it->first;
            int twosum = threesum - it->first;
            find_2sum(ans_set,ans,nums_map,twosum);
        }
        nums_map[ans[0]]++;
        return;
    }
    void find_2sum(vector<vector<int> >&ans_set,vector<int> ans,map<int,int>&nums_map,int twosum)
    {
        nums_map[ans[1]]--;
        for(auto it=nums_map.find(ans[1]);it!=nums_map.end();it++)
        {
            if(it->second<=0)
            {
                continue;
            }
            int p = twosum - it->first;
            if(nums_map.count(p)>0)
            {
                if(p>it->first||p==it->first&&it->second>1)
                {
                    ans[2] = it->first;
                    ans[3] = p;
                    ans_set.push_back(ans);
                }
            }
        }
        nums_map[ans[1]]++;
        return;
    }
};

代码(思路二)

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        sort(nums.begin(),nums.end());
        vector<vector<int> >ans_set;
        vector<int> ans(4,0);
        int cnt = nums.size();
        for(int i = 0;i < cnt-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[cnt-3]+nums[cnt-2]+nums[cnt-1]continue;
            for(int j = i+1;j < cnt-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[cnt-2]+nums[cnt-1]continue;
                int p = j+1;
                int q = cnt-1;
                while(pif(nums[i]+nums[j]+nums[p]+nums[q]>target)
                    {
                        q--;
                        while(nums[q]==nums[q+1]&&pelse if(nums[i]+nums[j]+nums[p]+nums[q]while(nums[p]==nums[p-1]&&pelse
                    {
                        ans[0]=nums[i];
                        ans[1]=nums[j];
                        ans[2]=nums[p];
                        ans[3]=nums[q];
                        ans_set.push_back(ans);
                        q--;
                        while(nums[q]==nums[q+1]&&pwhile(nums[p]==nums[p-1]&&preturn ans_set;
    }
};

你可能感兴趣的:(LeetCode,TwoPointer,数组,Medium,leetcode)