|
23 | 23 | </strong>["MyCalendar","book","book","book"]
|
24 | 24 | [[],[10,20],[15,25],[20,30]]
|
25 | 25 | <strong>输出:</strong> [null,true,false,true]
|
26 |
| -<strong>解释:</strong> |
| 26 | +<strong>解释:</strong> |
27 | 27 | MyCalendar myCalendar = new MyCalendar();
|
28 |
| -MyCalendar.book(10, 20); // returns true |
| 28 | +MyCalendar.book(10, 20); // returns true |
29 | 29 | MyCalendar.book(15, 25); // returns false ,第二个日程安排不能添加到日历中,因为时间 15 已经被第一个日程安排预定了
|
30 |
| -MyCalendar.book(20, 30); // returns true ,第三个日程安排可以添加到日历中,因为第一个日程安排并不包含时间 20 |
| 30 | +MyCalendar.book(20, 30); // returns true ,第三个日程安排可以添加到日历中,因为第一个日程安排并不包含时间 20 |
31 | 31 | </pre>
|
32 | 32 |
|
33 | 33 | <p> </p>
|
@@ -57,15 +57,93 @@ MyCalendar.book(20, 30); // returns true ,第三个日程安排可以添加到
|
57 | 57 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
58 | 58 |
|
59 | 59 | ```python
|
| 60 | +from sortedcontainers import SortedDict |
60 | 61 |
|
| 62 | + |
| 63 | +class MyCalendar: |
| 64 | + |
| 65 | + def __init__(self): |
| 66 | + self.sd = SortedDict() |
| 67 | + |
| 68 | + def book(self, start: int, end: int) -> bool: |
| 69 | + idx = self.sd.bisect_right(start) |
| 70 | + if 0 <= idx < len(self.sd): |
| 71 | + if end > self.sd.values()[idx]: |
| 72 | + return False |
| 73 | + self.sd[end] = start |
| 74 | + return True |
| 75 | + |
| 76 | + |
| 77 | +# Your MyCalendar object will be instantiated and called as such: |
| 78 | +# obj = MyCalendar() |
| 79 | +# param_1 = obj.book(start,end) |
61 | 80 | ```
|
62 | 81 |
|
63 | 82 | ### **Java**
|
64 | 83 |
|
65 | 84 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 85 |
|
67 | 86 | ```java
|
| 87 | +import java.util.Map; |
| 88 | +import java.util.TreeMap; |
| 89 | + |
| 90 | +class MyCalendar { |
| 91 | + |
| 92 | + private final TreeMap<Integer, Integer> tm = new TreeMap<>(); |
| 93 | + |
| 94 | + public MyCalendar() { |
| 95 | + } |
| 96 | + |
| 97 | + public boolean book(int start, int end) { |
| 98 | + Map.Entry<Integer, Integer> ent = tm.floorEntry(start); |
| 99 | + if (ent != null && ent.getValue() > start) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + ent = tm.ceilingEntry(start); |
| 103 | + if (ent != null && ent.getKey() < end) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + tm.put(start, end); |
| 107 | + return true; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Your MyCalendar object will be instantiated and called as such: MyCalendar |
| 113 | + * obj = new MyCalendar(); boolean param_1 = obj.book(start,end); |
| 114 | + */ |
| 115 | +``` |
68 | 116 |
|
| 117 | +### **Go** |
| 118 | + |
| 119 | +```go |
| 120 | +type MyCalendar struct { |
| 121 | + rbt *redblacktree.Tree |
| 122 | +} |
| 123 | + |
| 124 | +func Constructor() MyCalendar { |
| 125 | + return MyCalendar{ |
| 126 | + rbt: redblacktree.NewWithIntComparator(), |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func (this *MyCalendar) Book(start int, end int) bool { |
| 131 | + if p, ok := this.rbt.Floor(start); ok && p.Value.(int) > start { |
| 132 | + return false |
| 133 | + } |
| 134 | + if p, ok := this.rbt.Ceiling(start); ok && p.Key.(int) < end { |
| 135 | + return false |
| 136 | + } |
| 137 | + this.rbt.Put(start, end) |
| 138 | + return true |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +/** |
| 143 | + * Your MyCalendar object will be instantiated and called as such: |
| 144 | + * obj := Constructor(); |
| 145 | + * param_1 := obj.Book(start,end); |
| 146 | + */ |
69 | 147 | ```
|
70 | 148 |
|
71 | 149 | ### **...**
|
|
0 commit comments