哈希表part01
day5-1 ● 哈希表理论基础
哈希表(散列表)
哈希函数 hashFunction = hashCode(x)%tableSize
哈希碰撞
拉链发
线性探测法
常见的三种哈希结构
数组
set(集合)
map(映射)
总结
牺牲空间换时间
day5-2 ● 242.有效的字母异位词
题意
哈希解法
相对位置
代码实现
class Solution {
/**
* 时间复杂度O(m+n)
* 空间复杂度O(1)
*/
public boolean isAnagram(String s, String t) {
int[] hash = new int[26];
for(int i = 0; i < s.length(); i++){
hash[s.charAt(i) - 'a']++;
}
for(int i = 0; i < t.length(); i++){
hash[t.charAt(i) - 'a']--;
}
for(int count : hash){
if(count != 0){
return false;
}
}
// 数组中的所有元素都为0,说明s和t是字母异味词
return true;
}
}
总结
数组也可实现hash,并且比较快
day5-3 ● 349. 两个数组的交集
解题思路
要去重
使用数组来做哈希是因为题目限制了数值的大小
set解决
class Solution {
// 使用hashSet
public int[] intersection(int[] nums1, int[] nums2) {
if(nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0){
return new int[0];
}
Set<Integer> set1 = new HashSet<>();
Set<Integer> resSet = new HashSet<>();
// 遍历数组1
for(int i : nums1){
set1.add(i);
}
for(int i : nums2){
if(set1.contains(i)){
resSet.add(i);
}
}
//return resSet.stream().mapToInt(x -> x).toArray();
int[] arr = new int[resSet.size()];
int j = 0;
for(int i : resSet){
arr[j++] = i;
}
return arr;
}
}
数组解决
class Solution {
// 使用hash数组
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);
}
}
// return resList.stream().mapToInt(x -> x).toArray();
int arr[] = new int[resList.size()];
int j = 0;
for(int i : resList){
arr[j++] = i;
}
return arr;
}
}
总结
注意条件限制
数组相对效率更高
day5-4 ● 202. 快乐数
思路
当遇到要快速判断一个元素是否出现在集合里的时候需要考虑哈希法
代码实现
class Solution {
public boolean isHappy(int n) {
Set<Integer> seen = new HashSet<>();
while(n != 1 && !seen.contains(n)){// 没有进入循环
seen.add(n);
n = getNext(n);
}
return n == 1;
}
private int getNext(int n){
int totalSum = 0;
while(n > 0){
int d = n % 10;
n = n / 10;
totalSum += d * d;
}
return totalSum;
}
}
day5-5 ● 1. 两数之和
整体思路
判断某个元素在集合中出现过,可以考虑使用哈希法
map解题过程
代码实现
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
if(nums == null || nums.length == 0){
return res;
}
// map 用来存放已经遍历过的元素
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
res[0] = map.get(temp);
res[1] = i;
}
// 将遍历过的元素存入map中
map.put(nums[i], i);
}
return res;
}
}
//使用双指针
public int[] twoSum(int[] nums, int target) {
int m=0,n=0,k,board=0;
int[] res=new int[2];
int[] tmp1=new int[nums.length];
//备份原本下标的nums数组
System.arraycopy(nums,0,tmp1,0,nums.length);
//将nums排序
Arrays.sort(nums);
//双指针
for(int i=0,j=nums.length-1;i<j;){
if(nums[i]+nums[j]<target)
i++;
else if(nums[i]+nums[j]>target)
j--;
else if(nums[i]+nums[j]==target){
m=i;
n=j;
break;
}
}
//找到nums[m]在tmp1数组中的下标
for(k=0;k<nums.length;k++){
if(tmp1[k]==nums[m]){
res[0]=k;
break;
}
}
//找到nums[n]在tmp1数组中的下标
for(int i=0;i<nums.length;i++){
if(tmp1[i]==nums[n]&&i!=k)
res[1]=i;
}
return res;
}
总结
- 为什么想要使用哈希法
- 为什么要用map
- map的作用:存放遍历过的元素
- map中的key:遍历的树,value:存数组的下标