当前位置:网站首页>[哈希表] 刷题合集
[哈希表] 刷题合集
2022-07-27 00:21:00 【哇咔咔负负得正】
参考
https://www.bilibili.com/video/BV1Ra41177RB
LeetCode 2206. 将数组划分成相等数对
https://leetcode.cn/problems/divide-array-into-equal-pairs/
bool divideArray(int* nums, int numsSize){
int hash[501];
memset(hash, 0, sizeof(hash));
bool ans = true;
for (int i = 0; i < numsSize; i++) {
hash[nums[i]]++;
}
for (int i = 0; i < 501; i++) {
if (hash[i] & 1) {
return false;
}
}
return ans;
}
LeetCode 1832. 判断句子是否为全字母句
https://leetcode.cn/problems/check-if-the-sentence-is-pangram/
bool checkIfPangram(char * sentence){
int n = strlen(sentence);
// 字母 ASCII 码均小于 128
int hash[128];
memset(hash, 0, sizeof(hash));
for (int i = 0; i < n; i++) {
hash[sentence[i]]++;
}
for (int i = 'a'; i <= 'z'; i++) {
if (hash[i] == 0) {
return false;
}
}
return true;
}
LeetCode 1512. 好数对的数目
https://leetcode.cn/problems/number-of-good-pairs/
遍历 hash 的方法:
int numIdenticalPairs(int* nums, int numsSize){
int hash[101];
memset(hash, 0, sizeof(hash));
int ans = 0;
for (int i = 0; i < numsSize; i++) {
hash[nums[i]]++;
}
for (int i = 0; i < 101; i++) {
if (hash[i] > 1) {
ans += (hash[i] - 1) * hash[i] / 2;
}
}
return ans;
}
更为方便的一次遍历(每次加入一个新的数,只需加上和前面所有相同数匹配的次数,即加上原来 hash 里面的值):
int numIdenticalPairs(int* nums, int numsSize){
int hash[101];
memset(hash, 0, sizeof(hash));
int ans = 0;
for (int i = 0; i < numsSize; i++) {
ans += hash[nums[i]];
hash[nums[i]]++;
}
return ans;
}
LeetCode 2006. 差的绝对值为 K 的数对数目
https://leetcode.cn/problems/count-number-of-pairs-with-absolute-difference-k/
与 “1512. 好数对的数目” 类似的做法:
int countKDifference(int* nums, int numsSize, int k){
int hash[101];
memset(hash, 0, sizeof(hash));
int ans = 0;
for (int i = 0; i < numsSize; i++) {
// 两个判断是为了防止越界
ans += nums[i] + k <= 100 ? hash[nums[i] + k] : 0;
ans += nums[i] >= k ? hash[nums[i] - k] : 0;
hash[nums[i]]++;
}
return ans;
}
LeetCode 930. 和相同的二元子数组
https://leetcode.cn/problems/binary-subarrays-with-sum/
先求前 n 项和数组 sum ,
即 sum[j] 为前 j 项的和,sum[j] - sum[i] 表示 a[i+1] ~ a[j] 的和。
即求满足 sum[j] == sum[i] + goal 的全部子串,其中 i ∈ [ − 1 , j − 1 ] i \in [-1, j-1] i∈[−1,j−1]。
hash[ sum[i] + goal ] 表示 a[i+1] ~ a[j] 的所有满足条件子串数目。
初始化 hash[sum(-1) + goal] ++ 即 hash[goal] = 1。
解法一:
int numSubarraysWithSum(int* nums, int numsSize, int goal){
int hash[60001];
memset(hash, 0, sizeof(hash));
int ans = 0;
// 计算前缀和,即前 n 项和
for (int i = 1; i < numsSize; i++) {
nums[i] += nums[i-1];
}
hash[goal] = 1;
for (int i = 0; i < numsSize; i++) {
ans += hash[nums[i]];
hash[nums[i] + goal]++;
}
return ans;
}
解法二:使用省内存的写法
int numSubarraysWithSum(int* nums, int numsSize, int goal){
int hash[30001];
memset(hash, 0, sizeof(hash));
int ans = 0;
// 计算前缀和,即前 n 项和
for (int i = 1; i < numsSize; i++) {
nums[i] += nums[i-1];
}
hash[0] = 1;
for (int i = 0; i < numsSize; i++) {
if (nums[i] >= goal) {
ans += hash[nums[i] - goal];
}
hash[nums[i]]++;
}
return ans;
}
LeetCode 560. 和为 K 的子数组
https://leetcode.cn/problems/subarray-sum-equals-k/
数据较小可以直接用数组表示 hash,数据较大可以使用 C++ STL 中的 unordered_map。
参考 930. 和相同的二元子数组 解法一(时间和空间稍大):
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map <int , int> hash;
int ans = 0;
int n = nums.size();
// 计算前 n 项和
for (int i = 1; i < n; i++) {
nums[i] += nums[i-1];
}
hash[k] = 1;
for (auto &num : nums) {
ans += hash[num];
hash[num+k]++;
}
return ans;
}
};
参考 930. 和相同的二元子数组 解法二(时间和空间稍小):
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map<int, int> hash;
hash[0] = 1;
int ans = 0, sum_n = 0;
for (auto& num : nums) {
sum_n += num;
if (hash.find(sum_n - k) != hash.end()) {
ans += hash[sum_n - k];
}
hash[sum_n]++;
}
return ans;
}
};
LeetCode 454. 四数相加 II
https://leetcode.cn/problems/4sum-ii/
只许判定 -(nums1[i] + nums2[j]) 是否与 (nums3[k] + nums4[l]) 相等即可。
class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
unordered_map <int, int> hash;
int ans = 0;
int a = nums1.size();
int b = nums1.size();
int c = nums1.size();
int d = nums1.size();
for (int i = 0; i < a; ++i){
for (int j = 0; j < b; ++j) {
hash[-(nums1[i] + nums2[j])]++;
}
}
for (int i = 0; i < c; ++i){
for (int j = 0; j < d; ++j) {
ans += hash[nums3[i] + nums4[j]];
}
}
return ans;
}
};
LeetCode 160. 相交链表
https://leetcode.cn/problems/intersection-of-two-linked-lists/
解法一 :
@sylin:走到尽头见不到你,于是走过你来时的路,等到相遇时才发现,你也走过我来时的路。
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == NULL || headB == NULL) {
return NULL;
}
ListNode *pa = headA;
ListNode *pb = headB;
while (pa != pb) {
pa = pa == NULL ? headB : pa->next;
pb = pb == NULL ? headA : pb->next;
}
return pa;
}
};
解法二 hash:
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
unordered_map <ListNode *, bool> hash;
ListNode *pa = headA;
ListNode *pb = headB;
while (pa) {
hash[pa] = true;
pa = pa->next;
}
while (pb) {
if (hash.find(pb) != hash.end()) {
return pb;
}
pb = pb->next;
}
return NULL;
}
};
边栏推荐
- 使用 WebSocket 实现一个网页版的聊天室(摸鱼更隐蔽)
- Debezium系列之:基于debezium offset拉取历史数据,确保数据没有丢失
- F8 catch traffic, F9 catch rabbits, f10turttle
- Leetcode skimming -- no.238 -- product of arrays other than itself
- Non global function of lua function
- 调用JShaman的Web API接口,实现JS代码加密。
- CS224W fall 课程 ---- 1.1 why Graphs ?
- 云开发寝适闹钟微信小程序源码
- JMeter interface test, quickly complete a single interface request
- Ten thousand words long text, take you to understand the kubernetes network model
猜你喜欢

OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks

static关键字

Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion

Functions of libraries and Archives

Interview shock 68: why does TCP need three handshakes?

com.fasterxml.jackson.databind.exc.InvalidDefinitionException

JMeter interface test, quickly complete a single interface request

【Redis】快速入门

论构造函数的原型是谁

Web3.0世界知识体系分享-什么是Web3.0
随机推荐
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
LeetCode刷题——NO.238——除自身以外数组的乘积
Rust Web(一)—— 自建TCP Server
基于GoLang实现API短信网关
CS224W fall 1.2 Applications of Graph ML
Debezium series: pull historical data based on debezium offset to ensure that data is not lost
阿里云解决方案架构师张平:云原生数字化安全生产的体系建设
Cs224w fall course - --- 1.1 why graphs?
Leetcode- > binary search clock in
Arduinouno drive RGB module full color effect example
What did kubedmin do?
哈希表与一致性哈希的原理理解以及应用
Lua函数之非全局函数
F8 catch traffic, F9 catch rabbits, f10turttle
c语言:深度学习递归
"Software testing" packaging resume directly improves the pass rate from these points
I heard that you knelt on the interface test during the interview?
com.fasterxml.jackson.databind.exc.InvalidDefinitionException
Swiperjs custom width