File tree 1 file changed +21
-14
lines changed
1 file changed +21
-14
lines changed Original file line number Diff line number Diff line change 1
- struct MyCalendar { }
1
+ pub struct MyCalendar {
2
+ books : Vec < ( i32 , i32 ) > ,
3
+ }
2
4
3
- /**
4
- * `&self` means the method takes an immutable reference.
5
- * If you need a mutable reference, change it to `&mut self` instead.
6
- */
7
5
impl MyCalendar {
8
- fn new ( ) -> Self { }
6
+ pub fn new ( ) -> Self {
7
+ Self { books : vec ! [ ] }
8
+ }
9
9
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
+ }
11
24
}
12
25
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
-
19
26
#[ cfg( test) ]
20
27
mod tests {
21
28
use super :: * ;
22
29
23
30
#[ test]
24
31
fn example_1 ( ) {
25
- let obj = MyCalendar :: new ( ) ;
32
+ let mut obj = MyCalendar :: new ( ) ;
26
33
assert ! ( obj. book( 10 , 20 ) ) ;
27
34
assert ! ( !obj. book( 15 , 25 ) ) ;
28
35
assert ! ( obj. book( 20 , 30 ) ) ;
You can’t perform that action at this time.
0 commit comments