Leetcode 1814. Count Nice Pairs in an Array

2021-07-23 16:21:58 浏览数 (1)

1. Description

2. Solution

**解析:**Version 1,由题目可知nums[i] - rev(nums[i])相等的两个数即可构成一个合适的对,因此统计nums[i] - rev(nums[i])相等的数值即可。

  • Version 1
代码语言:javascript复制
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

  1. https://leetcode.com/problems/count-nice-pairs-in-an-array/

0 人点赞