594. Longest Harmonious Subsequence
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cnt={}
for num in nums:
if num not in cnt:
cnt[num]=1
else:
cnt[num]+=1
res=0
for k in cnt.keys():
if k-1 in cnt:
res=max(cnt[k-1]+cnt[k],res)
if k+1 in cnt:
res=max(cnt[k+1]+cnt[k],res)
return res

浙公网安备 33010602011771号