Skip to content

Commit 60942bc

Browse files
authored
feat: add rust solution to lc problem: No.0252 (#1238)
1 parent cb3592e commit 60942bc

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

solution/0200-0299/0252.Meeting Rooms/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,43 @@ public:
9898
};
9999
```
100100
101+
### **Rust**
102+
103+
```rust
104+
impl Solution {
105+
#[allow(dead_code)]
106+
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
107+
if intervals.len() == 1 {
108+
return true
109+
}
110+
111+
let mut intervals = intervals;
112+
113+
// Sort the intervals vector
114+
intervals.sort_by(|lhs, rhs| {
115+
lhs[0].cmp(&rhs[0])
116+
});
117+
118+
let mut end = -1;
119+
120+
// Begin traverse
121+
for p in &intervals {
122+
if end == -1 {
123+
// This is the first pair
124+
end = p[1];
125+
continue;
126+
}
127+
if p[0] < end {
128+
return false;
129+
}
130+
end = p[1];
131+
}
132+
133+
true
134+
}
135+
}
136+
```
137+
101138
### **Go**
102139

103140
```go

solution/0200-0299/0252.Meeting Rooms/README_EN.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,43 @@ public:
7373
};
7474
```
7575
76+
### **Rust**
77+
78+
```rust
79+
impl Solution {
80+
#[allow(dead_code)]
81+
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
82+
if intervals.len() == 1 {
83+
return true
84+
}
85+
86+
let mut intervals = intervals;
87+
88+
// Sort the intervals vector
89+
intervals.sort_by(|lhs, rhs| {
90+
lhs[0].cmp(&rhs[0])
91+
});
92+
93+
let mut end = -1;
94+
95+
// Begin traverse
96+
for p in &intervals {
97+
if end == -1 {
98+
// This is the first pair
99+
end = p[1];
100+
continue;
101+
}
102+
if p[0] < end {
103+
return false;
104+
}
105+
end = p[1];
106+
}
107+
108+
true
109+
}
110+
}
111+
```
112+
76113
### **Go**
77114

78115
```go
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn can_attend_meetings(intervals: Vec<Vec<i32>>) -> bool {
4+
if intervals.len() == 1 {
5+
return true
6+
}
7+
8+
let mut intervals = intervals;
9+
10+
// Sort the intervals vector
11+
intervals.sort_by(|lhs, rhs| {
12+
lhs[0].cmp(&rhs[0])
13+
});
14+
15+
let mut end = -1;
16+
17+
// Begin traverse
18+
for p in &intervals {
19+
if end == -1 {
20+
// This is the first pair
21+
end = p[1];
22+
continue;
23+
}
24+
if p[0] < end {
25+
return false;
26+
}
27+
end = p[1];
28+
}
29+
30+
true
31+
}
32+
}

0 commit comments

Comments
 (0)