Directory
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 theindex
of the hash table. Then we can query fast whether this student is in this school by theindex
. - There are two methods to solve hash collision:
Zipper Method
andLinear Collision Method
- There there three types of data structures:
array
、set
、map
LeetCode 242. Valid Anagram
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 ins
- Traverse
t
, perform-1
operation on the value on the has table index correaponding to the characters appearing ist
- Iterate
record
, if exist an element that doesn’t equal to0
, returnfalse
LeetCode 349. Intersection of Two Arrays
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 thanarray
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 isunordered
. - Convert a set to an array:
res.stream().mapToInt(x->x).toArray()
LeetCode 202. Happy Numbe
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 ofn
after every lopping. - Notice how to calculate the next value of
n
.
LeetCode 1. Two Sum
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 thekey
to store element value and usevalue
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