Code Random Record Algorithm Training Camp 15th Day 7 | 454. Addition of Four Numbers II, 383. Ransom Letter, 15. Sum of Three Numbers, 18. Sum of Four Numbers

I watched something else yesterday, so I didn't practice clocking in yesterday. Today I will make up for yesterday's learning knowledge.

454. Adding Four Numbers II

Suggestion: This question is a problem solved by using map. Let’s have a good experience of how the hash method can improve program execution efficiency and reduce time complexity. Of course, using hash method will increase the space complexity, but generally speaking, we trade space for time. , the same is true for industrial development.

Topic Link: Lituo

Idea: We are given four arrays, find an element in each of the four arrays, and the result is 0, and there is no need to perform deduplication. You can first traverse the first two arrays A+B, and then traverse the latter two arrays C+D. If the sum of the four elements is equal to 0, the result element can be obtained. Considering that the subscript of the array is relatively large, considering map and set, it is not only necessary to count whether it has occurred, but also to count the number of occurrences, so here we use map to solve this problem.

map key:a+b value:3

Here 0-(c+d)=a+b is selected. When the thinking is clear, it is relatively simple.

Why is it divided into two parts, the process of traversing the first two is n^2, and then the latter is also n^2. If it is divided into 1 and 3 types, it is the case of n and n^3.

The use of undered_map here is not sensitive to duplicate data and is highly efficient. See test code below

class Solution {
public:
    int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {

        //首先,声明一个map,里面放置很多自己想要的数据,map的key就是相应的a+b的数值,value是a+b出现的次数
        unordered_map<int,int> mymap;

        for(int a:nums1)
        {
            for(int b:nums2)
            {
                mymap[a+b]++;//相当是value值想加
            }
        }

        int count = 0;
        for(int c:nums3)
        {
            for(int d:nums4)
            {
                if(mymap.find(0-(c+d))!=mymap.end())
                {
                    count += mymap[(0-(c+d))];

                }
                
            }
        }
        return count;



    }
};

383. Ransom letter  

Suggestion: This question and 242. Effective anagrams are an idea, which can be regarded as an extended question.

Topic Link: Lituo

The following code is written by myself, which is easier to understand.

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {

        int record[26]={0};

        if(ransomNote.size()>magazine.size())
        {
            return false;
        }


        for(int i=0;i<ransomNote.size();i++)
        {
            record[ransomNote[i]-'a']++;
        } 

        for(int i=0;i<magazine.size();i++)
        {
            record[magazine[i]-'a']--;
        } 

        for(int i=0;i<ransomNote.size();i++)
        {
            if(record[ransomNote[i]-'a']>0)
            {
                return false;
            }
            
        } 

        return true;


    }
};

15. Sum of three numbers 

Suggestion: Although this question is very similar to the sum of two numbers, you can also use the hash method, but it will be troublesome to use the hash method. The double-pointer method is the correct solution. You can watch the video to understand the idea of ​​the double-pointer method. The article explains Yes, no problem. Hashing is messy.

Topic Link: Lituo

This place is a little more complicated than the two situations, a+b+c=0; this question needs to be deduplicated, so the hash method cannot be used, and the sorting method can be used to solve it first.

The key here is to deduplicate, because there cannot be duplicates in the result set. 

Steps: ①First sort, then traverse, if it is greater than 0, directly perform a return;
②Deduplicate operation, judge num[i]==num[i+1] and num[i]==num[i-1] Note that there may be repetitions in the result set, so use num[i]==num[i-1] to perform a continue operation when traversing. ③Note that left<right cannot be =. ④Judge whether the
condition
is Established, if established, harvest the results.
⑤Prevent the occurrence of {-1,-1,-1,-1,0,0,0,0,0,1,1,1,1,1,1} .

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {

        vector<vector<int>> result;
        sort(nums.begin(), nums.end());
        
        for (int i = 0; i < nums.size(); i++) {
       
            if (nums[i] > 0) {
                return result;
            }
           
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int left = i + 1;
            int right = nums.size() - 1;
            while (right > left) {
               
                if (nums[i] + nums[left] + nums[right] > 0) right--;
                else if (nums[i] + nums[left] + nums[right] < 0) left++;
                else {
                    result.push_back(vector<int>{nums[i], nums[left], nums[right]});
                    
                    while (right > left && nums[right] == nums[right - 1]) right--;
                    while (right > left && nums[left] == nums[left + 1]) left++;

               
                    right--;
                    left++;
                }
            }

        }
        return result;

    }
};

18. The sum of four numbers ---- I don't understand, I will read it later

Suggestion: To compare, the difference between this question and 454. Addition of Four Numbers II, and why 454. Addition of Four Numbers II is much simpler. If you understand this, you will have a deeper understanding of this question. The overall idea of ​​this question is the same as the sum of three numbers, both are double pointers, but there are many small details when writing, and you need to pay attention. It is recommended to watch the video first.

Topic Link: Lituo

Idea: On the basis of the above question, a loop condition is added. The process of adding four numbers to equal the target has some details (pruning and deduplication operations)

Guess you like

Origin blog.csdn.net/m0_47489229/article/details/130973496