Skip to content

Commit ae1372e

Browse files
committed
feat: resolved
1 parent b25d205 commit ae1372e

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

my_calendar_i/src/lib.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
struct MyCalendar {}
1+
pub struct MyCalendar {
2+
books: Vec<(i32, i32)>,
3+
}
24

3-
/**
4-
* `&self` means the method takes an immutable reference.
5-
* If you need a mutable reference, change it to `&mut self` instead.
6-
*/
75
impl MyCalendar {
8-
fn new() -> Self {}
6+
pub fn new() -> Self {
7+
Self { books: vec![] }
8+
}
99

10-
fn book(&self, start: i32, end: i32) -> bool {}
10+
pub fn book(&mut self, start: i32, end: i32) -> bool {
11+
let mut i = 0;
12+
for (j, (s, e)) in self.books.iter().enumerate() {
13+
if *e <= start {
14+
continue;
15+
};
16+
if end > *s {
17+
return false;
18+
}
19+
i = j;
20+
}
21+
self.books.insert(i, (start, end));
22+
true
23+
}
1124
}
1225

13-
/**
14-
* Your MyCalendar object will be instantiated and called as such:
15-
* let obj = MyCalendar::new();
16-
* let ret_1: bool = obj.book(start, end);
17-
*/
18-
1926
#[cfg(test)]
2027
mod tests {
2128
use super::*;
2229

2330
#[test]
2431
fn example_1() {
25-
let obj = MyCalendar::new();
32+
let mut obj = MyCalendar::new();
2633
assert!(obj.book(10, 20));
2734
assert!(!obj.book(15, 25));
2835
assert!(obj.book(20, 30));

0 commit comments

Comments
 (0)