题意:分出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)

龙仔
12 声望4 粉丝