楼主:
dont 2024-11-13 19:25:442563. Count the Number of Fair Pairs
## 思路
任两数相加介于lower, upper之间
先排序
用Two pointer分别计算小于等于upper跟lower-1 Pair个数并相减
## Code
```
class Solution:
def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int:
nums.sort()
n = len(nums)
def get_count(val):
res = 0
left, right = 0, n-1
while left < right:
if nums[left] + nums[right] <= val:
res += right - left
left += 1
else:
right -= 1
return res
return get_count(upper) - get_count(lower-1)
```