![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
哈希表
Vaccy Zhu
自律是解决人生问题最主要的工具,也是解决人生痛苦最重要的方法!
展开
-
确定两个字符串是否接近-哈希表1657-c++
【代码】确定两个字符串是否接近-哈希表1657-c++原创 2024-03-31 12:47:11 · 94 阅读 · 0 评论 -
找出两数组的不同-哈希表2215-c++
【代码】找出两数组的不同-哈希表-c++原创 2024-03-27 23:52:35 · 87 阅读 · 0 评论 -
同构字符串-字符串205-python
没看答案,需要构造两个映射哈希表(s->t和t->s)。class Solution: def isIsomorphic(self, s: str, t: str) -> bool: n = len(s) mapping_s = {} mapping_t = {} for i in range(n): if s[i] not in mapping_s.keys():原创 2022-05-05 14:38:27 · 101 阅读 · 0 评论 -
独一无二的出现次数-哈希表1207-python
没看答案,首先利用哈希表统计元素出现次数,然后对次数进行排序,最后查验是否出现了重复。from collections import defaultdictclass Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: freq = defaultdict(int) for num in arr: freq[num] += 1 freq =原创 2022-05-04 15:06:13 · 671 阅读 · 0 评论 -
两个数组的交集II-哈希表350-python
没看答案。考察了取两个字典的交集操作。from collections import Counterclass Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: a = Counter(nums1) b = Counter(nums2) res = [] c = a & b for nu原创 2022-02-17 20:29:47 · 215 阅读 · 0 评论 -
查找共用字符-哈希表1002-python
考察了两个哈希表去交集的操作。整体思路是统计出每个字符串里26个字符的出现的频率,然后取每个字符频率最小值,最后转成输出格式即可。from collections import Counterclass Solution: def commonChars(self, words: List[str]) -> List[str]: c = Counter(words[0]) res = [] for word in words:原创 2022-02-17 20:24:02 · 291 阅读 · 0 评论 -
字母异位词分组-哈希表49-python
由于互为字母异位词的两个字符串包含的字母相同,因此两个字符串中的相同字母出现的次数一定是相同的,故可以将每个字母出现的次数使用数组存储,作为哈希表的键。由于字符串只包含小写字母,因此对于每个字符串,可以使用长度为 26 的数组记录每个字母出现的次数。需要注意的是,在使用数组作为哈希表的键时,不同语言的支持程度不同,因此不同语言的实现方式也不同。from collections import defaultdictclass Solution: def groupAnagrams(self,原创 2022-02-16 14:24:23 · 236 阅读 · 0 评论 -
和为 K 的子数组-数组560-python&c++
前缀和算法: 可以O(1)时间下得到子数组的和。pythonclass Solution: def subarraySum(self, nums: List[int], k: int): n = len(nums) preNum = [0] * (n+1) needs = {0:1} count = 0 for i in range(n): preNum[i+1] = preNum[i]原创 2021-09-08 20:07:41 · 267 阅读 · 0 评论 -
和等于 k 的最长子数组长度-数组325-python
pythonclass Solution: def maxSubArrayLen(self, nums: List[int], k: int): n = len(nums) needs = {0:[0]} res = 0 preNum = 0 for i in range(1, n+1): preNum += nums[i-1] target = pre原创 2021-09-08 20:59:44 · 213 阅读 · 0 评论 -
最长连续序列-数组128-python
并查集:在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并。pythonclass Solution: def longestConsecutive(self, arr: List[int]): n = len(arr) res = 0 dic = {} for data in arr: tmp = 0原创 2021-08-14 23:19:52 · 144 阅读 · 0 评论 -
前K个高频单词-哈希表692-python
pythonclass Solution: def topKFrequent(self, words: List[str], k: int): dic = {} res = [] for ch in words: if dic.get(ch, -1) == -1: dic[ch] = 1 else: dic[ch] +=原创 2021-08-09 17:40:56 · 84 阅读 · 0 评论 -
赎金信-哈希表383-C++
算法思想:和LeetCode 242思想一样,只不过要把magazine字符串形成哈希表uomap,并在哈希表中查找ransomNote字符串的字符。class Solution {public: bool canConstruct(string ransomNote, string magazine) { unordered_map<char, int> uomap; for(auto ch : magazine){原创 2021-04-07 22:11:44 · 129 阅读 · 0 评论 -
四数相加II-哈希表454-C++
算法思想:C++class Solution {public: int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) { unordered_map<int, int> uomap; int res = 0; for(auto m : A){原创 2021-04-07 18:08:04 · 74 阅读 · 0 评论 -
快乐数-哈希表202-C++
算法思想:定义取n的各位数字的函数position和求各位数字平方和的函数multiple;用一个集合uomap来帮助判断while循环过程中有没有环的出现,如果有的话证明会是无限循环且一定不是快乐数,所以循环会停止,并return false;如果暂时没发现循环,则搜索是否出现各位数字平方和为1的答案,若搜索到了则return true,或出现环了则return false。C++class Solution {public: vector<int> posi原创 2021-04-07 17:52:30 · 178 阅读 · 0 评论 -
两个数组的交集-哈希表349-C++
算法思想:一、没看答案用一个哈希表uomap存储其中一个数组的值,然后遍历另一个数组并在uomap中查找;如找到了并且res中没有该值,则push_back到res中,否则继续循环。C++class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_map<int, int原创 2021-04-07 17:26:49 · 126 阅读 · 0 评论 -
有效的字母异位词-哈希表242-C++
算法思想:没看答案。先判断s和t的长度是否相等,不相等直接返回false;将s的字符存储在哈希表map中,int为出现的次数;遍历t并在map中查找,若找到了则该键值-1,以防s和t都出现某个字符但该字符出现在t的次数比s多。C++class Solution {public: bool isAnagram(string s, string t) { if(s.length() != t.length()) return false;原创 2021-04-06 19:58:54 · 77 阅读 · 0 评论 -
数组中出现次数超过一半的数字-哈希表(剑指offer39)-C++
class Solution {public: int majorityElement(vector<int>& nums) { int number = nums.size() / 2 + 1; unordered_map<int, int> uomap; int res = 0; for(auto v : nums){ uomap[v]++; }原创 2021-03-28 21:16:04 · 80 阅读 · 0 评论 -
两数之和-哈希表1-python&c++
运用哈希表class Solution: def twoSum(nums, target): hashtable = dict() for i, num in enumerate(nums): if target - num in hashtable: return [hashtable[target - num], i] hashtable[nums[i]] = i原创 2021-02-04 19:16:59 · 139 阅读 · 1 评论