Skip to content

Commit a4c6ecc

Browse files
authored
feat: add swift implementation to lcof2 problem: No.058 (doocs#3101)
1 parent 0e66859 commit a4c6ecc

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lcof2/剑指 Offer II 058. 日程表/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,37 @@ func (this *MyCalendar) Book(start int, end int) bool {
150150
*/
151151
```
152152

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+
153184
<!-- tabs:end -->
154185

155186
<!-- solution:end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
*/

0 commit comments

Comments
 (0)