问题描述
解题思路
首先,变换一下题目的需求,nums[i] - rev(nums[i]) == nums[j] - rev(nums[j])
,然后利用哈希表记录每个值出现了多少次就可以了。
代码
class Solution {
public:
int rev(int num) {
vector<int> tmp;
int ans = 0;
while (num != 0) {
tmp.push_back(num % 10);
num /= 10;
}
for (int i = 0; i < tmp.size(); i++) {
ans = ans * 10 + tmp[i];
}
return ans;
}
int countNicePairs(vector<int>& nums) {
map<int, int> mp;
for (int i = 0; i < nums.size(); i++) {
mp[nums[i] - rev(nums[i])]++;
}
long long ans = 0;
for (auto it = mp.begin(); it != mp.end(); it++) {
ans += (long long) it->second * (it->second - 1) / 2;
}
return ans % (1000000007);
}
};
标签:1814,nums,int,rev,long,num,ans,中好,对子
From: https://www.cnblogs.com/zwyyy456/p/17479220.html