LeetCode Brushing Diary Hash Table I

1. Effective anagrams

topic description

insert image description here

problem solving ideas

This question can be solved using the idea of ​​a hash table. There are 26 letters in a word. We can define an array with a length of 26 to represent a hash table, where each letter and the ASCII value of the letter a represent the position of the 26 letters. , you can use two loops, count the number of occurrences of each letter for the first time, subtract the number of occurrences of each letter in the second loop, and finally determine whether the number of occurrences of each letter is not 0.

var isAnagram = function(s, t) {
   const countArr = new Array(26).fill(0)
   const zero = 'a'.charCodeAt()
   for(let i = 0;i<s.length;i++){
        countArr[s[i].charCodeAt() - zero]++
   }
   for(let i = 0;i<t.length;i++){
       countArr[t[i].charCodeAt() - zero] --
   }


   for(let i = 0;i<countArr.length;i++) {
       if(countArr[i]!==0){ // 一定要判断不等于0 之前判断小于0 会漏掉t字符串比s字符串长的情况
           return false
       }
   }

   return true
};

2. Intersection of two arrays

topic description

insert image description here

problem solving ideas

This question uses a hash table. The first cycle counts the number of occurrences of each number in the first array, and the second cycle determines whether each number has appeared in the hash table. If it has appeared, put this number into the final result array , it should be noted that the final result needs to be deduplicated.

var intersection = function(nums1, nums2) {
  let set = new Set(nums1)
    let res = []
    for(let i = 0;i<nums2.length;i++){
        if(set.has(nums2[i])){
            res.push(nums2[i])
        }
    }
    return [...new Set(res)]
};

3. Happy Number

topic description

insert image description here

problem solving ideas

The difficulty of this question is to find the sum of the squares of each digit. The rest is to judge whether the sum has appeared in the hash table. If it has appeared, it means that there will be an infinite loop and you can directly return false. If the sum is 1, return true.

var isHappy = function(n) {
  let set = new Set() // 本题只需要存值即可 所以使用Set数据集
  while(1) {
      let sum = getSum(n)
      if(set.has(sum)) {
          return false
      } else if(sum == 1) {
          return true
      } else {
          set.add(sum)
      }
      n = sum
  }
};
var getSum = function(n) {
    let sum = 0;
    while(n) {
        sum += (n%10)**2 // n % 2 求末位数
        n = Math.floor(n/10); // n/10 整除
    }
    return sum
}

4. The sum of two numbers

topic description

insert image description here

problem solving ideas

It is relatively simple to use a hash table in this question. We need to give an element to determine whether this element has appeared. If it has, return the subscript of this element.
Then to determine whether an element appears, this element will be used as a key, so the element in the array is used as a key, and the key corresponds to the value, and the value is used to store the subscript.

var twoSum = function(nums, target) {
  let map = new Map()
  for(let i = 0;i<nums.length;i++){
      if(map.has(target -nums[i])) {
          return [i,map.get(target-nums[i])]
      } else {
          map.set(nums[i],i)
      }
  }
};

Guess you like

Origin blog.csdn.net/Salange1/article/details/128294604
Recommended