第三章 哈希表part01
今日任务
- 哈希表理论基础
- 242.有效的字母异位词
- 349.两个数组的交集
- 202.快乐数
- 1.两数之和
详细布置
哈希表理论基础
建议:大家要了解哈希表的内部实现原理,哈希函数,哈希碰撞,以及常见哈希表的区别,数组,set 和map。
什么时候想到用哈希法,当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法。 这句话很重要,大家在做哈希表题目都要思考这句话。
文章讲解:https://programmercarl.com/%E5%93%88%E5%B8%8C%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html
242.有效的字母异位词
建议: 这道题目,大家可以感受到 数组 用来做哈希表 给我们带来的遍历之处。
题目链接/文章讲解/视频讲解: https://programmercarl.com/0242.%E6%9C%89%E6%95%88%E7%9A%84%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D.html
进阶: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?
答:unicode 用2个字节统一进行编码 所以将unordered_map<char,int> 改为 unordered_map<string,int> 即可
注意unordered_map的妙用
c++:
class Solution {
public:
bool isAnagram(string s, string t) {
unordered_map<char,int> a,b;
for(char c : s) a[c] ++;
for(char c : t) b[c] ++;
return a == b;
}
};
java:
用两个哈希表
class Solution {
public boolean isAnagram(String s, String t) {
HashMap<Character, Integer> a = new HashMap<>();
HashMap<Character, Integer> b = new HashMap<>();
for(int i = 0;i < s.length();i++){
a.put(s.charAt(i),a.getOrDefault(s.charAt(i),0)+1);
}
for(int i = 0;i < t.length();i++){
b.put(t.charAt(i),b.getOrDefault(t.charAt(i),0)+1);
}
return a.equals(b);
}
}
用一个哈希表
该函数用于判断两个字符串是否为互为字母异位词(anagram)。其功能可概括为以下几点:
- 初始化一个HashMap<Character, Integer>
a
,用于记录字符及其在字符串s
中的出现次数。 - 遍历字符串
s
,对每个字符,将其在a
中的计数增加1(若不存在则默认为0)。 - 遍历字符串
t
,对每个字符,在a
中的计数减少1(若不存在则默认为0)。 - 遍历
a
中所有存储的字符计数值,若存在任一非零值,则表明s
和t
不是字母异位词,函数返回false
。 - 若遍历结束后所有计数值均为0,则表明
s
和t
是字母异位词,函数返回true
。
public boolean isAnagram(String s, String t) {
HashMap<Character, Integer> a = new HashMap<>();
for(int i = 0;i < s.length();i++){
a.put(s.charAt(i),a.getOrDefault(s.charAt(i),0)+1);
}
for(int i = 0;i < t.length();i++){
a.put(t.charAt(i),a.getOrDefault(t.charAt(i),0)-1);
}
for(int val : a.values()){
if(val != 0){
return false;
}
}
return true;
}
349. 两个数组的交集
建议:本题就开始考虑 什么时候用set 什么时候用数组,本题其实是使用set的好题,但是后来力扣改了题目描述和 测试用例,添加了 0 <=nums1[i], nums2[i] <= 1000 条件,所以使用数组也可以了,不过建议大家忽略这个条件。 尝试去使用set。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0349.%E4%B8%A4%E4%B8%AA%E6%95%B0%E7%BB%84%E7%9A%84%E4%BA%A4%E9%9B%86.html
c++:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
unordered_set<int> Set;
for(int x : nums1) Set.insert(x);
for(int x : nums2){
//发现 nums2 的元素在nums1出现过
if(Set.count(x)){
res.push_back(x);//将其放入答案中
Set.erase(x);//同时将集合中的元素去除
}
}
return res;
}
};
该函数用于求两个整数数组(nums1
和nums2
)的交集,并返回一个整数数组表示交集的结果。以下是详细步骤:
- 初始化一个HashSet集合
set
,并将nums1
数组的所有元素加入到集合中,利用哈希集合特性去重并加速查找操作。 - 创建一个ArrayList列表
list
,用于存储两个数组的交集元素。 - 遍历
nums2
数组,对于其中的每个元素x
,检查set
是否包含x
:- 若
set
包含x
,则将x
添加到list
中,并从set
中移除已处理的交集元素,防止重复添加。
- 若
- 创建一个新的整数数组
res
,其长度与存放交集元素的list
相同。 - 再次遍历
list
,将列表中的元素依次复制到res
数组中。 - 最后,返回填充了交集元素的
res
数组。
java:
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set = new HashSet<>();
for(int x : nums1){
set.add(x);
}
ArrayList<Integer> list = new ArrayList<>();
for(int x : nums2){
if(set.contains(x)){
list.add(x);
set.remove(x);
}
}
int[] res = new int[list.size()];
for(int i = 0;i < list.size();i++){
res[i] = list.get(i);
}
return res;
}
202. 快乐数
建议:这道题目也是set的应用,其实和上一题差不多,就是 套在快乐数一个壳子
题目链接/文章讲解:https://programmercarl.com/0202.%E5%BF%AB%E4%B9%90%E6%95%B0.html
这道题也可以用快慢指针的思想判断循环(可以不用使用到哈希表)
为了熟悉set,下面还是基于set写
c++:
class Solution {
public:
// 取数值各个位上的单数之和
int get(int n)
{
int res = 0;
while(n != 0){
res += (n % 10) * (n % 10);
n /= 10;
}
return res;
}
bool isHappy(int n) {
unordered_set<int> Set;
while(1)
{
int sum = get(n);
if(sum == 1) return true;
// 如果这个sum曾经出现过,说明已经陷入了无限循环了,立刻return false
if(Set.count(sum)) return false;
else Set.insert(sum);
//继续下一轮
n = sum;
}
}
};
java:
class Solution {
public boolean isHappy(int n) {
HashSet<Integer> set = new HashSet<>();
while(n != 1 && set.contains(n) == false){
set.add(n);
n = get(n);
}
return n == 1;
}
//计算给定整数的各个位数的平方和
private int get(int n){
int res = 0;
while(n != 0){
res += (n % 10) * (n % 10);
n /= 10;
}
return res;
}
}
1. 两数之和
建议:本题虽然是 力扣第一题,但是还是挺难的,也是 代码随想录中 数组,set之后,使用map解决哈希问题的第一题。
建议大家先看视频讲解,然后尝试自己写代码,在看文章讲解,加深印象。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0001.%E4%B8%A4%E6%95%B0%E4%B9%8B%E5%92%8C.html
(哈希表) O(n)
使用C++中的哈希表——unordered_map hash
.
循环一遍 nums数组,在每步循环中我们做两件事:
- 判断 target−nums[i]是否在哈希表中;
- 将 nums[i] 插入哈希表中;
解释:由于数据保证有且仅有一组解,假设是 [i,j](i<j)[ , ]( < ),则我们循环到 j 时,nums[i]一定在哈希表中 一定在哈希表中,且有 nums[i]+nums[j]==target, 所以我们一定可以找到解。
时间复杂度:由于只扫描一遍,且哈希表的插入和查询操作的复杂度是 O(1) ,所以总时间复杂度是 O(n).
c++:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> hash;
for(int i = 0;i < nums.size();i++)
{
if(hash.count(target - nums[i])){
return {hash[target - nums[i]],i};
}else
{
hash[nums[i]] = i;
}
}
return {};
}
java:
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> hash = new HashMap<>();
for(int i = 0;i < nums.length;i++){
int x = target - nums[i];
if(hash.containsKey(x)){
return new int[]{hash.get(x),i};
}else{
hash.put(nums[i],i);
}
}
return new int[]{};
}
}