2023-07-08 LeetCode daily question (sum of three numbers)

2023-07-08 one question per day

1. Topic number

15. 三数之和

2. Topic link

Click to jump to the topic location

3. Topic description

Given an integer array nums, determine whether there is a triple [nums[i], nums[j], nums[k]] that satisfies i != j, i != k and j != k, and also satisfies nums[ i] + nums[j] + nums[k] == 0 . please

You return all triples that sum to 0 and are not duplicates.

**Note:** The answer cannot contain repeated triplets.
hint:

  • 3 <= nums.length <= 3000
  • -105 <= nums[i] <= 105

4. Problem solving code

class Solution {
    
    
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
    
    
        vector<vector<int>> ret;
        sort(nums.begin(), nums.end());
        int n = nums.size();
        if(nums[0] + nums[1] + nums[3] > 0){
    
    
            return ret;
        }
        for(int i = 0; i < n; ++i){
    
    
            if(i != 0 && nums[i] == nums[i-1]){
    
    
                continue;
            }
            int left = i + 1;
            int right = n - 1;
            while(left < right){
    
    
                int temp = nums[i] + nums[left] + nums[right];
                if(temp == 0){
    
    
                    ret.push_back({
    
    nums[i], nums[left], nums[right]});
                    left++;
                    right--;
                    while(left < right && nums[left] == nums[left - 1]){
    
    
                        ++left;
                    }
                    while(left < right && nums[right] == nums[right + 1]){
    
    
                        --right;
                    }
                } else if(temp < 0){
    
    
                    ++left;
                } else{
    
    
                    --right;
                }
            }
        }      
    return ret;
    }
};

Five, problem-solving ideas

(1) This topic is to find all combinations of three numbers that add up to 0 from the array, then the problem is transformed into <1> how to find the number <2> how to not repeat.

(2) If you use the simplest triple loop method to enumerate violently, the time complexity must exceed the scope of the system's judgment, and it must be wrong, so you have to find other methods. When encountering such problems, we generally use Sort by way of advanced preprocessing. Sort the array numerically from smallest to largest. And we already know that the array must have at least three elements, so we first judge whether the sum of the smallest three numbers is greater than 0. If the sum of the smallest three numbers is greater than 0, then there must be no triples that meet the requirements.

(3) Traverse the array through the pointer i to find the first number. If i is not equal to 0 and nums[i] is equal to nums[i-1], skip the judgment (because the first number is repeated).

(4) The first number is determined, and the remaining two numbers are obtained in the interval [i + 1, n - 1]. Double pointers are used, left points to i+1, and right points to n-1. Note that temp is equal to nums[i] + nums[left] + nums[right], if temp is equal to 0, it means a result, left++, right–, and then judge whether nums[left] is equal to nums[left - 1] and nums Whether [right] is equal to nums[right + 1], if it is equal, the pointer will continue to move, and as long as left < right, the pointer can move. If temp is less than 0, it means that the number is small, and the left pointer moves right. If temp is greater than 0, it means that the number is too large, and the right pointer moves to the left.

(5) Finally return the result array.

Guess you like

Origin blog.csdn.net/qq_56086076/article/details/131618985