class Solution:
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
n = len(hand)
if n % groupSize != 0: # 须得满足牌的个数是分组的倍数
return False
d = {}
hand.sort() # 排序
L = []
for num in hand: # 哈希表存入每个牌数出现的次数
if num in d:
d[num] += 1
else:
d[num] = 1
L.append(num) # 列表存无重复的牌数
for l in L:
while d[l] != 0: # 遍历牌数,在哈希表中挑出与之连续的能成一组的牌,
d[l] -= 1
j = 1
while j < groupSize:
if l + j in d and d[l+j] > 0:
d[l+j] -= 1
j += 1
else:
return False
return True
Leetcode 846
最新推荐文章于 2024-11-02 14:05:28 发布