1. 程式人生 > >leetcode python實現 731. My Calendar II

leetcode python實現 731. My Calendar II

題目傳送門:

思路:構造一個字典記錄每個數字出現的次數,每次進來一個樣本,都先對上次的字典進行備份(萬一不通過就回溯)

class MyCalendarTwo:

    def __init__(self):
        self.dic = {}

    def book(self, start, end):
        """
        :type start: int
        :type end: int
        :rtype: bool
        """
        not_continue = self.dic.copy()
        for i in range(start,end):
            if i not in self.dic.keys():
                self.dic[i] = 1
            elif self.dic[i] == 2:
                self.dic = not_continue
                print(self.dic)
                return False
                break
            else:
                self.dic[i] = self.dic[i]+1
        return True