#LeetCode 242. Valid Anagram
#LeetCode 242. 视频讲解:学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词_哔哩哔哩_bilibili
Anagram定义: An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
用哈希法的情况:判断一个元素是否在集合中,此题目满足条件。
哈希法共有三种表现方式:数组、set集合、map映射。数组在此题中有更快的速度,所以选用数组。
将第一个字符串中的字母分别映射到哈希树上,用record 数组来记录每个字母的数量,在record 数组中将第二个字符串中出现的字母减去。如果record 数组不全部为0,则说明两个字符串存在不同的字母。
哈希数组方法:
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'] += 1;
}
for (int j = 0; j < t.length(); j++) {
record[t.charAt(j) - 'a'] -= 1;
}
for (int k = 0; k < 26; k++) {
if (record[k] != 0) {
return false;
}
}
return true;
}
}
#LeetCode 349. Intersection of Two Arrays
#LeetCode 349. 视频讲解:学透哈希表,set使用有技巧!Leetcode:349. 两个数组的交集_哔哩哔哩_bilibili
寻找两数组的交集即为寻找元素是否在集合中,考虑使用哈希法。
使用HashSet情况:数组非常大,会占用很多内存;数组不大,但是元素很分散,例如:[1, 0, 1000000] 。HashSet 只能保存一个key 值。
HashSet方法:第一个数组中的元素依次保存在HashSet中,对比第二个数组中的元素,如果存在重复(使用contains(key) 判断),则记录在result 这个HashSet 中(使用HashSet 可以去除重复值)用于返回,在return 之前需要将HashSet 转换为数组。
数组方法:将第一个数组中的元素依次保存在哈希数组中,将其设置为1 (如果元素存在,在哈希数组中即为1 )。遍历第二个数组,如果第二个数组中的值在此哈希数组中显示为1,则代表存在此元素。
HashSet方法:
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
// HashSet method
if (nums1 == null || nums1.length == 0 ||
nums2 == null || nums2.length == 0) {
return new int[0];
}
Set<Integer> set1 = new HashSet<>();
Set<Integer> result = new HashSet<>();
for (int i = 0; i < nums1.length; i++) {
set1.add(nums1[i]);
}
for (int j = 0; j < nums2.length; j++) {
if (set1.contains(nums2[j])) {
result.add(nums2[j]);
}
}
int[] result_arr = new int[result.size()];
int count = 0;
for (int k: result) {
result_arr[count++] = k;
}
return result_arr;
}
}
数组方法:
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
// Array method
int[] hash1 = new int[1007];
HashSet<Integer> result = new HashSet<>();
for (int i = 0; i < nums1.length; i++) {
hash1[nums1[i]] = 1;
}
int count = 0;
for (int j = 0; j < nums2.length; j++) {
if (hash1[nums2[j]] == 1) {
result.add(nums2[j]);
}
}
int[] hash_result = new int[result.size()];
for (int k: result) {
hash_result[count++] = k;
}
return hash_result;
}
}
#LeetCode 202. Happy Number
#LeetCode 202. 文字讲解:代码随想录
此题使用哈希法原因:判断一个元素(在此题中是sum)是否在集合中。如果sum 重复出现,则为false ,如果sum = 1 则停止遍历。
使用HashSet 存sum ,判断是否重复,使用ArraySet 存数字的digits ,因为ArraySet 允许重复。
HashSet方法:
class Solution {
public boolean isHappy(int n) {
HashSet<Integer> sum = new HashSet<>();
int sum_temp = 0;
while (sum_temp != 1) {
ArrayList<Integer> number = new ArrayList<>();
sum_temp = 0;
while (n >= 10) {
int digit = n % 10;
number.add(digit);
n = n / 10;
}
number.add(n);
for (int k : number) {
sum_temp += k * k;
}
n = sum_temp;
if (sum.contains(sum_temp)) {
return false;
}
sum.add(sum_temp);
}
return true;
}
}
#LeetCode 1. Two Sum
#LeetCode 1. 视频讲解:梦开始的地方,Leetcode:1.两数之和,学透哈希表,map使用有技巧!_哔哩哔哩_bilibili
用数组和set 做哈希法会受到限制的情况:数组的大小受限;set 是集合,只能保存一个key 。
使用哈希表的原因:查找遍历过的元素,与目前的元素相加是否能等于target 。建立的HashMap 的用于保存遍历过且不满足条件的元素。HashMap 可以保存两个值(key, value) ,在这个题目中key 用于保存元素的值,value 用于保存数组的下标。containsKey(key) 判断是否有这个值(题目中用于判断是否存在与目前的元素相加等于target 的值),get(key) 得到的是key 对应的value (题目中获得key 值的下标),put(key, value) 放入新值(不满足的元素放入HashMap )。
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> number = new HashMap<>();
int[] result = new int[2];
if(nums == null || nums.length == 0){
return result;
}
for (int i = 0; i < nums.length; i++) {
int temp = target - nums[i];
if (number.containsKey(temp)) {
result[0] = number.get(temp);
result[1] = i;
return result;
}
number.put(nums[i], i);
}
return result;
}
}