[Code Random Records | Leetcode | Day 8] Hash Table | Valid Anagrams | Intersection of Two Arrays | Sum of Two Numbers

foreword

Welcome to Little K 's Leetcode|Code Random Thoughts | Thematic column, today I will bring you hashing~effective anagrams|intersection of two arrays|share of the sum of two numbers✨


242. Valid anagrams

✨Title link click here
Given two strings s and t, write a function to determine whether t is an anagram of s. Note: If the number of occurrences of each character in s and t is the same, then s and t are said to be anagrams of each other.

Example 1:

Input : s = “anagram”, t = “nagaram”
Output : true

Example 2:

Input : s = "rat", t = "car"
Output : false

hint:

1 <= s.length, t.length<= 5 * 104
s and t contain only lowercase letters

Ideas :

We use the hash method to solve this problem, and use the array as a hash table (the strings are lowercase letters and their ASCII code values ​​​​are continuous), so how do we directly define an array with a size of 26
?
It is how to a-zmap to 0-25, we can directly subtract a from the current value.
The specific method
is to initialize the array to 0, and then traverse the string 1. When the character appears once, the element at the corresponding position is incremented by one; similarly traverse the string 2. When the string appears once, the element at the corresponding position is decremented by one. If the elements of the array are all 0 at the end, it means that string 1 and string 2 are anagrams of each other, otherwise returnfalse

class Solution {
    
    
public:
    bool isAnagram(string s, string t) {
    
    
        int record[26] = {
    
    0};
        for (int i = 0; i < s.size(); i++) record[s[i]-'a']++;
        for (int j = 0; j < t.size(); j++) record[t[j]-'a']--;
        for (int k = 0; k < 26; k++) {
    
    
            if (record[k] != 0)
                return false;
        }
        return true;
    }
};

insert image description here


349. Intersection of Two Arrays

✨Title link hereGiven
two arrays nums1and nums2, return their intersection. Each element in the output must be unique. We can ignore the order of the output results.

Example 1:
Input : nums1 = [1,2,2,1], nums2 = [2,2]
Output : [2]

Example 2:
Input : nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output : [9,4]
Explanation : [4,9] is also passable

hint:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000

[Method 1]: Use unordered_set, store the value of an array into it, then traverse the second array, find the value of the second array in the first array, if found, insert it into the result array

Tips:
C++ set find - function to find an element with a given value val. Returns an iterator pointing to the element if the element is found, otherwise returns an iterator pointing to the end of the collection, i.e. set::end().

class Solution {
    
    
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
    
    
        unordered_set<int> result;
        unordered_set<int> nums(nums1.begin(),nums1.end());
        for (int num : nums2) {
    
    
            if (nums.find(num) != nums.end()) {
    
    
                result.insert(num);
            }
        }
        return vector<int>(result.begin(),result.end());
    }
};

insert image description here
[Method 2]: Use an array as a hash table, because the length of the array here is within 1000

class Solution {
    
    
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
    
    
        int hashTable[1005] = {
    
    0};
        unordered_set<int> result;
        for (int a : nums1) hashTable[a] = 1;
        for (int b : nums2) {
    
    
            if (hashTable[b] == 1) {
    
    
                result.insert(b);
            }
        }
        return vector<int>(result.begin(),result.end());
    }
};

insert image description here


1. The sum of two numbers

✨Title link point hereGiven
an integer array numsand an integer target value target, please find target the two integers whose sum is the target value in the array, and return their array subscripts. You can assume that there is only one answer for each input. However, the same element in the array cannot appear repeatedly in the answer. You can return answers in any order.

Example 1:

Input : nums = [2,7,11,15], target = 9
Output : [0,1]
Explanation : Because nums[0] + nums[1] == 9, return [0, 1].

Example 2:

Input : nums = [3,2,4], target = 6
Output : [1,2]

Example 3:

Input : nums = [3,3], target = 6
Output : [0,1]

hint:
2 <= nums.length<= 104
-109 <= nums[i]<= 109
-109 <= target<= 109
There will only be one valid answer

[Method 1]: Violence, two layers forof circulation directly dry

class Solution {
    
    
public:
    vector<int> twoSum(vector<int>& nums, int target) {
    
    
        for (int i = 0; i < nums.size(); i++) {
    
    
            for (int j = i+1; j < nums.size(); j++) {
    
    
                if (nums[i] + nums[j] == target) {
    
    
                    return {
    
    i,j};
                }
            }
        }
        return {
    
    };
    }
};

insert image description here

[Method 2]: Hash method

class Solution {
    
    
public:
    vector<int> twoSum(vector<int>& nums, int target) {
    
    
        unordered_map<int,int> map;
        for (int i = 0; i < nums.size(); i++) {
    
    
            auto iter = map.find(target - nums[i]);
            if (iter != map.end()){
    
    
                return {
    
    iter->second,i};
            }
            map.insert(pair<int, int>(nums[i], i));
        }
        return {
    
    };
    }
};

insert image description here

Summarize

When you need to quickly determine whether an element appears in the set, consider hash~~
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_72157449/article/details/131813665