关于2Sum,3Sum的题解

Posted xy913741894

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于2Sum,3Sum的题解相关的知识,希望对你有一定的参考价值。

题目网上有更详细的描述,这类题有一些通用的解法,我们先从2Sum看起:

Given nums = [2, 7, 2, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,

直接暴力的解法就是2重循环,O(n*n)的复杂度,我们可以使用更更高效的办法:

1.排序,使用左右指针,复杂度为nlogn

class Solution 
public:
    vector<int> twoSum(vector<int>& nums, int target) 
        vector<int> v;
        sort(nums.begin(), nums.end());
        int size = nums.size();
        int left = 0;
        int right = size-1;

        while (left < right)
        
            if (left == right)
                break;
            int sum = nums[left] + nums[right];
            if (sum > target)
                right--;
            else if (sum < target)
                left++;
            else
            
                v.push_back(nums[left]);
                v.push_back(nums[right]);
                left++;
                right--;
            
        

        return v;
    
;

2.因为a+b=target,对于每个元素a,只需要求target-a看是否在数组中,查找的话,可以采用排序+二分,复杂度为nlogn;如果使用hash复杂度会更快为n(需要考虑去重)

class Solution 
public:
    vector<int> twoSum(vector<int>& nums, int target) 
        unordered_map<int, int> m;
        vector<int> v;

        int size = nums.size();
        for (int i = 0; i < size; ++i)
        
            int numToFind = target - nums[i];
            if (m.find(numToFind) != m.end())//找到二元组存下标
            
                v.push_back(m[numToFind]);
                v.push_back(i);
            
            m[nums[i]] = i; //写在后面是为了去重
        
        return v;
    
;

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: 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) 
        vector<vector<int>> v;
        sort(nums.begin(), nums.end());
        int size = nums.size();
        for (int i = 0; i < size; i++)
        
            int target = -nums[i];
            int beg = i+1;
            int end = size-1;

            while (beg < end)
            
                if (beg == end)
                    break;
                int sum = nums[beg]+nums[end];
                if (sum > target)
                    --end;
                else if (sum < target)
                    ++beg;
                else
                
                    vector<int> tmp(3, 0);
                    tmp[0] = nums[i];
                    tmp[1] = nums[beg];
                    tmp[2] = nums[end];
                    v.push_back(tmp);
                    //删去重复元素
                    while (beg < end && nums[beg] == tmp[1])
                        ++beg;
                    while (beg < end && nums[beg] == tmp[2])
                        --end;
                

            
            while (i + 1 < nums.size() && nums[i + 1] == nums[i]) 
                i++;      
        
        return v;
    
;

以上是关于关于2Sum,3Sum的题解的主要内容,如果未能解决你的问题,请参考以下文章

3sum

3Sum

oracle列转行 WM_CONCAT LISTAGG

leetcode 2 sum& 3 sum

leetcode 4sum

Coursera Algorithms week1 Interview Questions: 3Sum in quadratic time