leetcode-846-Hand of Straights

题意:分出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)
    原文作者:龙仔
    原文地址: https://segmentfault.com/a/1190000016263929
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞