給出一個數(shù)組,問其中有多少個a,b數(shù)對,符合a+rev(b)=b+rev(a),rev為數(shù)字反轉(zhuǎn)
a+rev(b)=b+rev(a) -> a - rev(a) + b - rev(b) = 0,所以預(yù)處理所有a-rev(a),然后用Counter計算每種value出現(xiàn)次數(shù)然后累加
#1814
#Runtime: 586 ms (Beats 78.13%)
#Memory: 23 MB (Beats 43.75%)
class Solution(object):
def countNicePairs(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums = [x - int(str(x)[::-1]) for x in nums]
ans = 0
for x in Counter(nums).values():
ans = (ans + (x - 1) * x // 2) % (10**9 + 7)
return ans