LeetCode_哈希表

本文介绍了如何使用哈希表解决多种编程问题,包括判断字母异位词、查找常用字符、判断快乐数以及赎金信问题。通过哈希表存储字符串中字符的出现次数,可以有效地进行比较和查找操作。此外,还讨论了四数之和问题的优化解决方案,避免了暴力算法导致的效率低下。这些方法展示了哈希表在解决字符串和数组问题中的强大能力。
摘要由CSDN通过智能技术生成

力扣刷题(代码随想录顺序)

哈希表

242.有效的字母异位词

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

示例 1:
输入: s = “anagram”, t = “nagaram”
输出: true
非常简单,进行哈希,数组下表是a-z 值是出现次数,如果a的哈希表-b的哈希表全为0,return true;

class Solution {
public:
    bool isAnagram(string s, string t) {
        int hash[26];
        for(int i=0;i<=25;i++)
            hash[i]=0;

        for(int i=0;i<=s.size()-1;i++)
            hash[int(s[i])-97]++;

        for(int i =0;i<=t.size()-1;i++)
            hash[int(t[i])-97]--;

        for(int i =0;i<=25;i++)
            if(hash[i]!=0) return false;
            
        return true;
    }

};

1002. 查找常用字符

给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。

你可以按任意顺序返回答案。

【示例一】
输入:[“bella”,“label”,“roller”]
输出:[“e”,“l”,“l”]

和上体同样思路, 不过不相减,就简单的遍历,从列的视角看,如果都有大于0的值,那么就取其中最小值,完事!

class Solution {
public:
    vector<string> commonChars(vector<string>& words) {
        int n =words.size();
        int hash[n][26]={0};
        for(int i=0;i<=words.size()-1;i++){
            for(int j =0;j<=words[i].size()-1;j++){
                hash[i][words[i][j]-'a']+=1;
            }
        }
        int a[26]={0};
        for(int j =0;j<=25;j++){
            int temp=1;
            for(int i = 0;i<=n-1;i++){
                if(hash[i][j]==0) {
                    a[j]=0;
                    break;
                }
                else{
                    temp = temp<hash[i][j]? temp:hash[i][j];
                    a[j]=temp;
                }
            }
 
        }
        for(int i =0;i<=25;i++) cout<<a[i];
        cout<<endl;
        for(int i=0;i<=25;i++){
            if(a[i]==0) continue;
            else
                for(int j =1;j<=a[i];j++)
                    cout<<char(i+'a')<<endl;
        }
        return words;
    }
};

202快乐数

「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果 可以变为 1,那么这个数就是快乐数。

这道题不难主要是知道快乐数定义就可以了,
首先去百度找到快乐数的定义
不是快乐数的数称为不快乐数(unhappy number),所有不快乐数的数位平方和计算,最後都会进入 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循环中。
在十进位下,100以内的快乐数有(OEIS中的数列A00770) :1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100。
而快乐数就不会出现重复的,
所以只要把每一次的中间变量存储起来,然后放进map里面,如果找不到,放进去,找得到,return false;

class Solution {
public:
    int getsum(int n){
        int sum=0;
        while(n>0){
        sum+=(n%10)*(n%10);
        n=n/10;
        }      
        return sum;  
    }
    bool isHappy(int n) {
        unordered_set<int> set;
        while(true){
            n=getsum(n);
            if (n==1) return true;
            else
                if (set.find(n)==set.end())
                //说明不存在 找得到返回位置 找不到返回end
                    set.insert(n);
                else return false;
        }
    }
};

383赎金信

canConstruct(“a”, “b”) -> false canConstruct(“aa”, “ab”) -> false canConstruct(“aa”, “aab”) -> true
没什么好说的 哈希完事后大于0即可

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_map<char,int> temp1;
        for(char i : magazine)
            temp1[i]+=1;
        for(char i :ransomNote)
            if((temp1.find(i)==temp1.end()) or (temp1[i]==0) ) 
                return false;
            else temp1[i]-=1;
        return true;
    }
};

四数之和

例如:

输入:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
输出:
2
解释:
两个元组如下:

(0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
(1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

首先 暴力算法是过不了的

vector<int> res;
for(int i = 0;i<a.size();i++){
	for(int j=0;j<=b.size();j++{
		res.push_back(a[i]+b[j]);
	}
}

为什么呢 因为有些值是重复出现的啊,你暴力算法对a,b肯定是要遍历的,但是可以做一些优化使得接下来的c,d不用这么麻烦。

class Solution {
public:
    int  fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
        unordered_map<int,int> temp1;
        unordered_map<int,int> temp2;
        for(int a:nums1)
            for(int b:nums2)
                temp1[a+b]++;
        int count=0;
        for(int a:nums3)
            for(int b:nums4)
                if( temp1.find(0-a-b)!=temp1.end()  )
                    count+=temp1[0-a-b];
    cout<<count<<endl;
    return count;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值