2023-07-15 LeetCode daily question (sum of four numbers)

2023-7-15 one question per day

1. Topic number

18. 四数之和

2. Topic link

Click to jump to the topic location

3. Topic description

You are given an array nums of n integers, and a target value target. Please find and return quadruples [nums[a], nums[b], nums[c], nums[d]] that meet all the following conditions and are not repeated (if two quadruple elements correspond one-to-one , the two quadruples are considered repeated):

  • 0 <= a, b, c, d < n
  • a, b, c and d are different from each other
  • nums[a] + nums[b] + nums[c] + nums[d] == target
    You can return answers in any order .

hint:

  • 1 <= nums.length <= 200
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109

4. Problem solving code

class Solution {
    
    
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
    
    
        vector<vector<int>> res;
        int n = nums.size();
        if(n < 4){
    
    
            return res;
        }
        sort(nums.begin(), nums.end());
        for(int i = 0; i < n; ++i){
    
    
            while(i != 0 && i < n && nums[i] == nums[i-1]){
    
    
                ++i;
            }
            for(int j = i + 1; j < n; ++j){
    
    
                while(j != i + 1 && j < n && nums[j] == nums[j - 1]){
    
    
                    ++j;
                }
                int left = j + 1;
                int right = n - 1;
                while(left < right){
    
    
                    long long temp = (long long)nums[left] + nums[right] + nums[i] + nums[j];
                    if(temp < target){
    
    
                        ++left;
                    } else if(temp == target){
    
    
                        res.push_back({
    
    nums[i], nums[j], 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{
    
    
                        --right;
                    }
                }
            }
        }
    return res;
    }
};

Five, problem-solving ideas

(1) This topic is an advanced version of the sum of three numbers , which is not much different from the sum of three numbers. If you don’t know the sum of three numbers, you can read this article—— 2023-07-08 LeetCode Daily 1 Question (sum of three numbers)

(2) It is still the same way of thinking. Our problem is to find four numbers, and the ones that are not repeated are equal to the target. Then we sort the original array first, which is more conducive to us not to repeat.

(3) If we use the four-layer loop + hash judgment method, the time complexity is very high, and we must not be able to pass the question. So we have to choose another good tree to live in. We set the outermost loop to traverse the first number. To make the first number in the result different, then if the number is the same as the previous number, then all the possibilities for the number to be the value have been found out. It is not necessary The first number is also the same value, i++.

(4) At this time, we have transformed the problem into a problem of the sum of three numbers, but the sum of the numbers we are looking for is not equal to target, but equal to target - nums[i]. Because the sum of three numbers has been written in great detail in this article, so I won’t go into details. However, the general idea is the same as the idea in (3), how to avoid repetition, and then reduce the time complexity by using double pointers when there are only two numbers left.

Guess you like

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