ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode_哈希表

2021-11-07 11:30:35  阅读:125  来源: 互联网

标签:return int 快乐 temp1 vector 哈希 LeetCode


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

哈希表

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;
    }
};

标签:return,int,快乐,temp1,vector,哈希,LeetCode
来源: https://blog.csdn.net/HBkingnapoleon/article/details/121188929

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有