LeetCode quizzes--summary record of ideas

Hot questions 100 - 49. Grouping of allophones

  • Link:49. Letter anagram grouping - LeetCode
  • Idea:
    • Characteristics of letter anagrams: strings with the same letters but different arrangements ==> Therefore, they must be the same after sorting (because the number of each letter is the same)
    • Sort each word in alphabetical order. The newly formed word is used as the key of the hash table, and the original word is used as the value. Finally, the hash table can be constructed as follows:
      • ![[Pasted image 20231109110616.png]]
    • How to return hash.second answer?
      • Group hash.second according to key and push it into the vector array
      • Each element of a vector array is also a vector array.
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        //map<排序后单词,同排序的原单词数组>
        unordered_map<string,vector<string>> strVec;

        //1.给每个单词,按重排序后进行分组
        for(auto const &str:strs) //②
        {
            //1.1 ktmp:重排序后的新单词,作为key
            string ktmp = str;
            sort(ktmp.begin(),ktmp.end());
            //1.2 把原单词分到自己的小组
            strVec[ktmp].emplace_back(str); //①
        }

        //2.把分组结果取出,合并到res中
        vector<vector<string>> res;
        for(auto const &v:strVec)
        {
            res.emplace_back(v.second);
        }
        return res;
    }
};
  • optimization:
    • emplace_back()Japanesepush_back() distinction
      • After submitting, I checked the writing method with the same idea but a faster speed. I saw that emplace_back() was used instead of push_back(), so I compared the two to understand.
      • The difference between the two lies in the underlying implementation mechanism.
      • push_back()When adding an element to the end of a container, the element will first be created, and then the element will be copied or moved into the container (if copied, the previously created element will be destroyed afterwards)
      • When andemplace_back() are implemented, the element is created directly at the end of the container, eliminating the need to copy or move the element.
    • ② Or using it in autofor (auto const &s : strs) will also be faster
    • The optimization of ① and ② is to reduce copies, so it will be faster.

Hot Questions 100 - 1. Sum of two numbers

  • Link:1. Sum of two numbers - LeetCode

  • Idea:

    • Save each number as key and the corresponding subscript as value in the hash table; traverse each number num in the array again and find it one by onehash[target-num] ==> Because of the question Indicates that there must be an answer and it is unique ==> There are the following problems:
      • ① For two identical numbers, the subscript of the first number will be overwritten by the subscript of the second number.
      • ② There is a problem with the efficiency of traversing twice
    • So thinking: Since the question indicates that there must be an answer ==>, we use edge traversal and edge search.
      • If found, return directly
      • If it is not found, store it in the hash table first, because the current number may be matched by the following number.
  • detail:

    • ①To find whether the key value exists in the hash, you cannot find it based on whether the value corresponding to the key is 0: because when you look for a key that does not exist in the hash table, its value is also 0; it is easy to be mixed with a number whose subscript is exactly 0. ==> You have to use find
      • find() function: It will find whether a specific value exists in the container. If it does not exist, it will point to the end end;
      • find returns a pointer, and reading requires it->second; if the value is read based on the key, then iVal[target-nums[i]] is sufficient
      • Because the number before i is stored in the hash table, if you find it, you should first return the subscript of the number found in the hash table.
    • ③ Although the question indicates that there is one and only one solution, it still needsreturn{}
    • ④ Enumerate from scratch and store in the table if no match is found.
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        //创建哈希表
        unordered_map<int,int> iVal;
        
        //从数组头的下标开始遍历,i标记的是目标值在nums里的下标
        for(int i=0;i<nums.size();i++)
        {
          auto it=iVal.find(target-nums[i]); //①
          if(it!=iVal.end())
          {
            return {iVal[target-nums[i]],i};
            //②或return {it->second,i};
		  }else{
			  iVal[nums[i]] = i;//④ 从头枚举,没能匹配就存入表中
		  }
        }
        return {};//③
    }
};

おすすめ

転載: blog.csdn.net/SHIE_Ww/article/details/134308847