leetcode729. My Calendar I

来源:互联网 发布:抹茶美妆软件 编辑:程序博客网 时间:2024/04/30 05:05

设一个字典记录所有被预定的页面,然后就是判断区间相交了

当发生以下两种情况之一时认为区间相交

1、起点小于左端点且终点大于左端点

2、起点大于等于左端点且起点小于右端点

代码如下

class MyCalendar(object):    dic = {}    def __init__(self):        self.dic = {}    def book(self, start, end):        """        :type start: int        :type end: int        :rtype: bool        """        ok = True;        for left in self.dic:            right = self.dic[left]            if start < left and left < end:                ok = False            if left <= start and start < right:                ok = False;        if ok:            self.dic[start] = end        return ok# Your MyCalendar object will be instantiated and called as such:# obj = MyCalendar()# param_1 = obj.book(start,end)
原创粉丝点击