1. Description
2. Solution
**解析:**Version 1,由题目可知nums[i] - rev(nums[i])
相等的两个数即可构成一个合适的对,因此统计nums[i] - rev(nums[i])
相等的数值即可。
- Version 1
class Solution:
def countNicePairs(self, nums: List[int]) -> int:
count = 0
n = len(nums)
stat = {}
MODULE = 10**9 7
for i in range(n):
diff = nums[i] - int(str(nums[i])[::-1])
stat[diff] = stat.get(diff, 0) 1
if stat[diff] > 1:
count = stat[diff] - 1
return count % MODULE
Reference
- https://leetcode.com/problems/count-nice-pairs-in-an-array/