Day 6 | 242. Valid Anagram | 349. Intersection of Two Arrays | 202. Happy Numbe | 1. Two Sum

Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array


Basic Concept of Hash Table

  • A hash map usually was used to judge whether an element exists in a list.
  • Like if we want to know whether a student studies in this school. Through the hash function, we can convert the student name to hash code, which is the index of the hash table. Then we can query fast whether this student is in this school by the index.
  • There are two methods to solve hash collision: Zipper Method and Linear Collision Method
  • There there three types of data structures: arraysetmap

LeetCode 242. Valid Anagram

Question Link

Solution:

class Solution {
    public boolean isAnagram(String s, String t) {
    	// 
        int[] record = new int[26];
        for(int i = 0; i < s.length(); i++)
            record[s.charAt(i) - 'a']++; 
        
        for(int j =0; j < t.length(); j++)
            record[t.charAt(j) - 'a']--;
        
        for(int count : record){
            if(count != 0)
                return false;
        }
        return true;
    }
}

Thoughts:

  • Define a record array for recording the presenting time of every character.
  • Traverse s, perform +1 operation on the value on the hash table index corresponding to the characters appearing in s
  • Traverse t, perform -1 operation on the value on the has table index correaponding to the characters appearing is t
  • Iterate record, if exist an element that doesn’t equal to 0, return false

LeetCode 349. Intersection of Two Arrays

Question Link

Solution:

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        if(nums1 == null || nums1.length == 0 || nums2 == null|| nums2.length ==0)
            return new int[0];
        
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> res = new HashSet<>();
        for(int i : nums1)
            set1.add(i);
        for(int j : nums2) {
            if(set1.contains(j)){
                res.add(j);
            }
        }
        return res.stream().mapToInt(x->x).toArray();
    }
}

Thought:

  • If the number of the hash value is relatively small, especially scattered, and the span is very larger, using an array will cause a great waste of space. So in this question, we adopt set
  • But set not only takes up more space than array but also is slower than the array because the set needs to do the hash calculation.
  • In this question, HashSet doesn’t have multiple elements, and it is unordered.
  • Convert a set to an array: res.stream().mapToInt(x->x).toArray()

LeetCode 202. Happy Numbe

Question Link

Solution:

class Solution {
    public boolean isHappy(int n) {
        Set<Integer> record = new HashSet<>();
        while(n != 1){
            record.add(n);
            n = getNextNumber(n);
            if(record.contains(n))
                return false;
        }
        return true;
    }

    int getNextNumber(int n){
        int sum = 0;
        while(n!=0){
            int temp = n%10;
            sum += temp*temp;
            n /= 10;
        }
        return sum;
    }
}

Thoughts:

  • Define a record set to store the possible value of n after every lopping.
  • Notice how to calculate the next value of n.

LeetCode 1. Two Sum

Question Link

Solution:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        for(int i = 0; i < nums.length; i++){
            if(map.containsKey(target - nums[i])){
                res[0] = map.get(target - nums[i]);
                res[1] = i;
            }
            map.put(nums[i], i);
        }
        return res;
    }
}

Thoughts

  • We not only need to know the element value but also the index of the element. So the map is suitable. We can use the key to store element value and use value to store index.
  • When traversing the array, we just query the map to determine whether it has the corresponding value that adds up to target
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值