15. 3Sum [three and the number of]

Description
Given a nums n array of integers, determines whether there are three elements a, b, c nums such that a + b + c = 0? All the conditions are not satisfied to find duplicate triples.

Note: The answer can not contain duplicate triples.

Examples of
Here Insert Picture Description
ideas

You can refer to two and the number of ideas.

  • Two and the number of dictionaries (Improper, Because the same number may be multiple combinations need, and second, the number of and need only one answer, there is no such concerns)

For each person, the third person to pull the second person to look at this time becomes the sum of the two numbers, if the second person is needed, directly to the output, if the second person is not required, it is filled with the dictionary ( a second first individual not needed): d [0- first one - second person] = [first person, second person]

  • The number of pointers and two bis (need to sort, to determine how to move the pointer good)recommend

To sort, through the array, for each number, taking two pointers a, b, after which a is a number, a number is the last one, in a case where a <b, and if greater than target, the forward transfer B, If less than target, adjust back a, if, and for the target, this composition is added to the result
to avoid duplication trick:

  1. Outer loop, and if a current number as the number, skipped, because the choice of the number smaller than a number of the selection, directly encompasses
  2. For the inner loop, when a set of answers to find, if nums [b] === nums [b-1], skip b-1, if nums [a] == nums [a + 1], skip a + 1, because nums [i] has been determined, followed by a number that the number of the same, the combinations are repeated, it may be omitted

Reduce the calculation:

  1. If nums [i] + nums [i + 1] + nums [i + 2]> 0 break # or nums [i]> 0, then the outer loop ends. Minimum number> 0, the sum of the three and greater than 0, and the number of the latter than nums [i] is large, the composition has the latter problem,
  2. If nums [i] + nums [n -1] + nums [n-2] <target continue # is smaller than the maximum number of combinations for a next target number directly
    answer
  • python
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()#方便去重
        res = []
        n=len(nums)
        for i in range(n-2):
            if i>0 and nums[i]==nums[i-1]:
                continue
            if nums[i]+nums[i+1]+nums[i+2] > 0:#或者nums[i]>0
                break #最小的数>0,则三数之和大于0,且后面的数都比nums[i]大,则后面的组合都有问题,
            if nums[i]+nums[n-1]+nums[n-2]<0: continue
            a, b = i+1, n - 1
            
            while a <b:
                
                t = nums[a] + nums[i] + nums[b]
                if t < 0:
                    a += 1
                if t > 0:
                    b -= 1
                if t == 0:
                    res.append([nums[i], nums[a], nums[b]])
                    while a<b and nums[b]==nums[b-1]: 
                        b-=1
                    while a<b and nums[a]==nums[a+1]:
                        a+=1
                    a+=1
                    b-=1

        return res
  • c++
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> v;
        int n=nums.size();
        for (int i=0; i<n-2; i++)
        {
            int a=i+1,b=nums.size()-1;
            if (i>0 && nums[i]==nums[i-1]) continue;    
            if (nums[i]+nums[i+1]+nums[i+2]>0) break;//==nums[i]>0最小的数>0,则三数之和大于0,且后面的数都比nums[i]大,则后面的组合都有问题,        
            if (nums[i]+nums[n-1]+nums[n-2]<0) continue;
            while (a<b)
            {
                int t = nums[i]+nums[a]+nums[b];
                if (t<0) a++;
                if (t>0) b--;
                if (t==0)
                {
                    vector<int> temp = {nums[i],nums[a],nums[b]};
                    v.push_back(temp);
                    
                    while (a<b && nums[b]==nums[b-1]) b--;
                    while (a<b && nums[a]==nums[a+1]) a++;
                    a++;
                    b--;
                }
            }
        }
        return v;
    }
};
Published 78 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/puspos/article/details/103145575