Skip to content

Commit 563afbc

Browse files
committed
feat: add rust solution to lc problem: No.0729
No.0729.My Calendar I
1 parent e1afc08 commit 563afbc

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

Diff for: solution/0700-0799/0729.My Calendar I/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,50 @@ class MyCalendar {
209209
*/
210210
```
211211

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+
212256
### **...**
213257

214258
```

Diff for: solution/0700-0799/0729.My Calendar I/README_EN.md

+44
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,50 @@ class MyCalendar {
199199
*/
200200
```
201201

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+
202246
### **...**
203247

204248
```

Diff for: solution/0700-0799/0729.My Calendar I/Solution.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
*/

0 commit comments

Comments
 (0)