5708. Count Nice Pairs in an Array

有是被自己蠢哭的一天呀

本来想出来了思路,就差实现了,还挺高兴的。。。

结果看了一下dl的代码,,,emmm革命尚未成功,总算知道为什么大佬可以十几分钟写完我能写一个小时的代码(在思路完全一样的情况下)

class Solution {
public:
    long long  rev(long long x)
    {   long long res=0;
        while(x)
        {
            res=res*10+x%10;

            x/=10;
        }
        return res;
    }
   
    long long c(long long x)
    {
        return (x*(x-1))/2;
    }
    int countNicePairs(vector<int>& nums) {
        long long res=0;
        vector<long long> sup(nums.size());
        map<long long ,long> hash;
        for(long long i=0;i<nums.size();i++)
        {
            hash[nums[i]-rev(nums[i])]++;
        }
        for(auto x:hash)
        {
            res=(res+c(x.second))%1000000007;
        }
        return res;
    }
};

明明想到了用相同的差,,,其实hash确实更好,,要不我还得遍历一遍提高不少复杂度(这就是复杂度莫名其妙提高的原因吗)

为了自己,和那些爱你的人
原文地址:https://www.cnblogs.com/zhmlzhml/p/14616407.html