Leetcode:18. 4Sum

问题描述:
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums 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.

Example:

Given array nums = [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]
]

难度等级为中等

下面小伙伴们会看到一个很直接暴力的方法,没有用到任何的技巧:
这个纯粹是LZ为了好玩(^o^)/~

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {

        vector<vector<int> > solutions;
        sort(nums.begin(), nums.end());
        for(int i = 0; i < nums.size(); i++){
            for(int j = i+1; j < nums.size(); j++){
                for(int k = j+1; k < nums.size(); k++)
                {
                    for(int l = k+1; l < nums.size(); l++)
                    {

                        int sum = nums[i] + nums[j] + nums[k] + nums[l]; 
                        if(sum == target)
                        {
                            vector<int> one_solution;
                            one_solution.push_back(nums[i]);
                            one_solution.push_back(nums[j]);
                            one_solution.push_back(nums[k]);
                            one_solution.push_back(nums[l]);
                            solutions.push_back(one_solution);
                            if(solutions.size() >=2)
                            {
                                for(int m = 0; m < solutions.size()-1; m++)
                                {
                                    if(solutions[solutions.size()-1] == solutions[m])
                                    {
                                        solutions.pop_back();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return solutions;

    }
};

耗时为:635ms,这么一个小问题,耗时这么高应该是不能接受的。

下面的还是进行优化后的算法,运行的时间是19ms

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int> > res;
        if(nums.size() < 4)
            return res;
        sort(nums.begin(), nums.end());

        int n = nums.size();
        for(int i = 0; i < nums.size()-3; i++ ){
            if(i>0 && nums[i-1] == nums[i]) continue;
            if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3] > target) break;
            if(nums[i]+nums[n-1]+nums[n-2]+nums[n-3] < target) continue;

            for(int j = i+1; j < n-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[n-1]+nums[n-2] < target)continue;

                int left = j+1;
                int right = n-1;
                while(left<right)
                {
                    int sum = nums[i]+nums[j]+nums[left]+nums[right];
                    if(sum>target) right--;
                    else if(sum<target) left++;
                    else
                    {
                        res.push_back(vector<int>{nums[i], nums[j], nums[left], nums[right]});
                        do{left++;}while(nums[left]==nums[left-1] && left<right);
                        do{right--;}while(nums[right]==nums[right+1] && left<right);
                    }
                }
            }
        }
        return res;
    }
};

代码不难,静下心来读起来应该很好理解O(∩_∩)O哈哈~

猜你喜欢

转载自blog.csdn.net/felaim/article/details/80376617
今日推荐