题目浅析

  • 想查看原题可以点击题目链接

  • 简单地说,就是给一个整型数组,数组中分三种值,特殊数字,特殊数字的和,异常值,要求找到数组中可能要在的异常值的最大的值,并且异常值和特殊数字的和必须不同,但可以共享相同的值。

思路分享

代码解答(强烈建议自行解答后再看)

  • 参考题解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def getLargestOutlier(self, nums: List[int]) -> int:
# x + 2*y = total x是异常值,y是特殊数字和
# 如果尝试枚举 y,就得下面这样子
# rec = Counter(nums)
# total = sum(nums)
# max_num = -inf
# for num in nums:
# x = total-2*num
# if rec[x] and (x!=num or rec[x]>1):
# max_num = max(max_num, x)
# return max_num
# 如果想枚举 x
rec = Counter(nums)
total = sum(nums)
max_num = -inf
for x in nums:
num, left = divmod(total-x, 2)
if left == 0 and rec[num] and (x!=num or rec[num]>1):
max_num = max(max_num, x)
return max_num