---
# System prepended metadata

title: 1814. Count Nice Pairs in an Array
tags: [Leetcode, HashMap, Medium]

---

# 1814. Count Nice Pairs in an Array
###### tags: `Leetcode` `Medium` `HashMap`
Link: https://leetcode.com/problems/count-nice-pairs-in-an-array/description/
## 思路
和[2364. Count Number of Bad Pairs](https://hackmd.io/v9Qr8EE6ShmouCNoSZq00w)很像
```nums[i]+rev(nums[j])==nums[j]+rev(nums[i])```
```=> nums[i]-rev(nums[i])==nums[j]-rev(nums[j])```
## Code
```java=
class Solution {
    public int countNicePairs(int[] nums) {
        int n = nums.length;
        Map<Integer, Integer> map = new HashMap<>();
        int ans = 0;
        int mod = 1000000007;
        for(int i=0; i<n; i++){
            int rev = reverse(nums[i]);
            ans = (ans+map.getOrDefault(nums[i]-rev, 0))%mod;
            map.put(nums[i]-rev, map.getOrDefault(nums[i]-rev, 0)+1);
        }
        return ans;
    }
    public int reverse(int num){
        StringBuilder sb = new StringBuilder(String.valueOf(num));
        return Integer.valueOf(sb.reverse().toString());
    }
}
```