1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class Solution { public int countNicePairs(int[] nums) { int nicePairs = 0;
Map<Integer, Integer> countMap = new HashMap<>(); for (int num : nums) { int key = num - rev(num); int count = countMap.getOrDefault(key, 0); nicePairs += count; nicePairs %= 1000000000 + 7; countMap.put(key, count + 1); }
return nicePairs; }
private int rev(int num) { int res = 0; while (num > 0) { res = res * 10 + num % 10; num /= 10; } return res; } }
|
References
1814. Count Nice Pairs in an Array