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