File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed
lcof2/剑指 Offer II 058. 日程表 Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,37 @@ func (this *MyCalendar) Book(start int, end int) bool {
150
150
*/
151
151
```
152
152
153
+ #### Swift
154
+
155
+ ``` swift
156
+ class MyCalendar {
157
+
158
+ private var calendar: [(Int , Int )]
159
+
160
+ init () {
161
+ self .calendar = []
162
+ }
163
+
164
+ func book (_ start : Int , _ end : Int ) -> Bool {
165
+ let newEvent = (start, end)
166
+ let index = calendar.firstIndex { $0 .0 >= newEvent.1 } ?? calendar.count
167
+
168
+ if index > 0 && calendar[index - 1 ].1 > newEvent.0 {
169
+ return false
170
+ }
171
+
172
+ calendar.insert (newEvent, at : index)
173
+ return true
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Your MyCalendar object will be instantiated and called as such:
179
+ * let obj = MyCalendar()
180
+ * let ret = obj.book(start, end)
181
+ */
182
+ ```
183
+
153
184
<!-- tabs:end -->
154
185
155
186
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ class MyCalendar {
2
+
3
+ private var calendar : [ ( Int , Int ) ]
4
+
5
+ init ( ) {
6
+ self . calendar = [ ]
7
+ }
8
+
9
+ func book( _ start: Int , _ end: Int ) -> Bool {
10
+ let newEvent = ( start, end)
11
+ let index = calendar. firstIndex { $0. 0 >= newEvent. 1 } ?? calendar. count
12
+
13
+ if index > 0 && calendar [ index - 1 ] . 1 > newEvent. 0 {
14
+ return false
15
+ }
16
+
17
+ calendar. insert ( newEvent, at: index)
18
+ return true
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Your MyCalendar object will be instantiated and called as such:
24
+ * let obj = MyCalendar()
25
+ * let ret = obj.book(start, end)
26
+ */
You can’t perform that action at this time.
0 commit comments