题意:分出n组连续的W个元素的数组
思路:比较简单,直接循环删除连续的数组,如此while循环反复。
class Solution(object):
def isNStraightHand(self, hand, W):
# c = collections.Counter(hand)
hand.sort()
print(hand)
while hand:
try:
start=hand[0]
for i in range(W):
hand.remove(start+i)
except Exception as e:
return False
return True
if __name__=='__main__':
# hand = [1, 2, 3, 6, 2, 3, 4, 7, 8]
# W = 3
# hand = [1, 2, 3, 4, 5]
# W = 4
hand=[1,2,3,4,5,6]
W=2
st=Solution()
out=st.isNStraightHand(hand,W)
print(out)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。