File tree 3 files changed +127
-0
lines changed
solution/0700-0799/0729.My Calendar I
3 files changed +127
-0
lines changed Original file line number Diff line number Diff line change @@ -209,6 +209,50 @@ class MyCalendar {
209
209
*/
210
210
```
211
211
212
+ ### ** Rust**
213
+
214
+ ``` rust
215
+ use std :: collections :: BTreeMap ;
216
+
217
+ struct MyCalendar {
218
+ bt : BTreeMap <i32 , i32 >,
219
+ }
220
+
221
+ /**
222
+ * `&self` means the method takes an immutable reference.
223
+ * If you need a mutable reference, change it to `&mut self` instead.
224
+ */
225
+ impl MyCalendar {
226
+ fn new () -> Self {
227
+ MyCalendar {
228
+ bt : BTreeMap :: new (),
229
+ }
230
+ }
231
+
232
+ fn book (& mut self , start : i32 , end : i32 ) -> bool {
233
+ if let Some ((_ , & val )) = self . bt. range (..= start ). last () {
234
+ println! (" {} {} {}" , start , end , val );
235
+ if val > start {
236
+ return false ;
237
+ }
238
+ }
239
+ if let Some ((& key , _ )) = self . bt. range (start .. ). next () {
240
+ if key < end {
241
+ return false ;
242
+ }
243
+ }
244
+ self . bt. insert (start , end );
245
+ true
246
+ }
247
+ }
248
+
249
+ /**
250
+ * Your MyCalendar object will be instantiated and called as such:
251
+ * let obj = MyCalendar::new();
252
+ * let ret_1: bool = obj.book(start, end);
253
+ */
254
+ ```
255
+
212
256
### ** ...**
213
257
214
258
```
Original file line number Diff line number Diff line change @@ -199,6 +199,50 @@ class MyCalendar {
199
199
*/
200
200
```
201
201
202
+ ### ** Rust**
203
+
204
+ ``` rust
205
+ use std :: collections :: BTreeMap ;
206
+
207
+ struct MyCalendar {
208
+ bt : BTreeMap <i32 , i32 >,
209
+ }
210
+
211
+ /**
212
+ * `&self` means the method takes an immutable reference.
213
+ * If you need a mutable reference, change it to `&mut self` instead.
214
+ */
215
+ impl MyCalendar {
216
+ fn new () -> Self {
217
+ MyCalendar {
218
+ bt : BTreeMap :: new (),
219
+ }
220
+ }
221
+
222
+ fn book (& mut self , start : i32 , end : i32 ) -> bool {
223
+ if let Some ((_ , & val )) = self . bt. range (..= start ). last () {
224
+ println! (" {} {} {}" , start , end , val );
225
+ if val > start {
226
+ return false ;
227
+ }
228
+ }
229
+ if let Some ((& key , _ )) = self . bt. range (start .. ). next () {
230
+ if key < end {
231
+ return false ;
232
+ }
233
+ }
234
+ self . bt. insert (start , end );
235
+ true
236
+ }
237
+ }
238
+
239
+ /**
240
+ * Your MyCalendar object will be instantiated and called as such:
241
+ * let obj = MyCalendar::new();
242
+ * let ret_1: bool = obj.book(start, end);
243
+ */
244
+ ```
245
+
202
246
### ** ...**
203
247
204
248
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: BTreeMap ;
2
+
3
+ struct MyCalendar {
4
+ bt : BTreeMap < i32 , i32 > ,
5
+ }
6
+
7
+ /**
8
+ * `&self` means the method takes an immutable reference.
9
+ * If you need a mutable reference, change it to `&mut self` instead.
10
+ */
11
+ impl MyCalendar {
12
+ fn new ( ) -> Self {
13
+ MyCalendar {
14
+ bt : BTreeMap :: new ( ) ,
15
+ }
16
+ }
17
+
18
+ fn book ( & mut self , start : i32 , end : i32 ) -> bool {
19
+ if let Some ( ( _, & val) ) = self . bt . range ( ..=start) . last ( ) {
20
+ println ! ( "{} {} {}" , start, end, val) ;
21
+ if val > start {
22
+ return false ;
23
+ }
24
+ }
25
+ if let Some ( ( & key, _) ) = self . bt . range ( start..) . next ( ) {
26
+ if key < end {
27
+ return false ;
28
+ }
29
+ }
30
+ self . bt . insert ( start, end) ;
31
+ true
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Your MyCalendar object will be instantiated and called as such:
37
+ * let obj = MyCalendar::new();
38
+ * let ret_1: bool = obj.book(start, end);
39
+ */
You can’t perform that action at this time.
0 commit comments