我们定义一个和谐数组是一个数组,其最大值和最小值之间的差值恰好为1。
现在,给定一个整数数组,你需要在其所有可能的子序列中找出其最长的和谐子序列的长度。注意,和谐数组不一定要求连续
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ans = 0
c = collections.Counter(nums)
for x in c:
if x+1 in c:
ans = max(ans,c[x]+c[x+1])
return ans