这种情况有点抽象,感觉会出这种问题,是因为hash表的结构问题?求解!
该题的题解具体是:
https://leetcode.cn/problems/count-pairs-that-form-a-complete-day-ii/solutions/2961462/zhe-chong-qing-kuang-you-dian-chou-xiang-spc2/
Code
C++
class Solution {
public:
long long countCompleteDayPairs(vector<int>& hours) {
int n=hours.size();
for(int i=0;i<n;i++)
hours[i]%=24;
sort(hours.begin(),hours.end());
long long ans=0;
unordered_map<int,int>hash;
unordered_map<int,int>hash1;
for(auto c:hours)
{
if(c!=0&&c!=12)
hash1[c]++;
hash[c]++;
}
long long s_0=hash[0],s_12=hash[12];
ans += (s_0 * (s_0 - 1)) / 2;
ans += (s_12 * (s_12 - 1)) / 2;
long long as=0;
for(auto [a,b]:hash)/*把这里的auto [a,b]:hash里的hash改成hash1即可让该程序成功Ac,
但到底为什么会这样,求解?是和hash的结构体有关吗?具体是为什么?谢谢*/
{
if(a!=0&&a!=12)
{
int k=24-a;
as+=b*hash[k];
}
}
return as/2+ans;
}
};
作者:少时诵诗书
链接:https://leetcode.cn/problems/count-pairs-that-form-a-complete-day-ii/solutions/2961462/zhe-chong-qing-kuang-you-dian-chou-xiang-spc2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。