Fundamental Knowledge:
-
Time complexity: O(1), extra space as a compensation
-
Two data structures (Java): HashSet (
HashSet(<Type>,<Type>) = new HashSet();
), HashMap (HashMap(<Type>,<Type>) = new HashMap();
) -
Basic operations to a Hash Map:
Add/update key-value:put(key, value)
Get value:get(key)
orgetOrDefault(key, defaultValue)
Check key exists:ontainsKey(key)
Check value exists:containsValue(value)
Remove element:remove(key)
Check if empty:isEmpty()
Get size:size()
Iterate keys:keySet()
Iterate values:values()
Iterate entries:entrySet()
Clear all entries:clear()
-
Basic operations to a Hash Set:
Add element:add(value)
Remove element:remove(value)
Check if element exists:contains(value)
Check if empty:isEmpty()
Get size:size()
Iterate elements:forEach()
,iterator()
, or enhanced for-loop
Convert to array:toArray()
Clear all elements:clear()
Leetcode Exercises
242. Valid Anagram (Easy) (Link)
- Hash Map
- Sort Array
Solution 1: Hash Map
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
HashMap<Character, Integer> count = new HashMap<>();
for (char c : s.toCharArray()) {
count.put(c, count.getOrDefault(c, 0) + 1);
}
for (char c : t.toCharArray()) {
if (!count.containsKey(c) || count.get(c) == 0) return false;
count.put(c, count.get(c) - 1);
}
return true;
}
}
Solution 2: Sort Array
import java.util.Arrays;
public class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
char[] arr1 = s.toCharArray();
char[] arr2 = t.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
return Arrays.equals(arr1, arr2);
}
}
349. Intersection of Two Arrays (Easy) (Link)
- Hash Array (theoretically faster)
- Hash Set / Hash Map
Solution 1: Hash Array
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int[] hash1 = new int[1002];
int[] hash2 = new int[1002];
for(int i : nums1)
hash1[i]++;
for(int i : nums2)
hash2[i]++;
List<Integer> resList = new ArrayList<>();
for(int i = 0; i < 1002; i++)
if(hash1[i] > 0 && hash2[i] > 0)
resList.add(i);
int index = 0;
int res[] = new int[resList.size()];
for(int i : resList)
res[index++] = i;
return res;
}
}
Solution 2: Hash Set/Map
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> elements = new HashSet<>();
ArrayList<Integer> res = new ArrayList<>();
for (int i : nums1) {
elements.add(i);
}
for (int j : nums2) {
if (elements.contains(j)) {
res.add(j);
elements.remove(j);
}
}
int[] result = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
result[i] = res.get(i);
}
return result;
}
}
242. Valid Anagram (Easy) (Link)
Best Version Solution:
import java.util.HashSet;
class Solution {
public boolean isHappy(int n) {
HashSet<Integer> seen = new HashSet<>();
while (n != 1 && !seen.contains(n)) {
seen.add(n);
n = getSumOfSquares(n);
}
return n == 1;
}
private int getSumOfSquares(int num) {
int sum = 0;
while (num > 0) {
int digit = num % 10; // Extract last digit
sum += digit * digit;
num /= 10; // Remove last digit
}
return sum;
}
}
1. Two Sum (Easy) (Link)
Solution:
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
if(nums == null || nums.length == 0){
return res;
}
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int temp = target - nums[i]; // 遍历当前元素,并在map中寻找是否有匹配的key
if(map.containsKey(temp)){
res[1] = i;
res[0] = map.get(temp);
break;
}
map.put(nums[i], i); // 如果没找到匹配对,就把访问过的元素和下标加入到map中
}
return res;
}