Skip to content

Commit 9bead11

Browse files
authored
feat: add rust solution to lc problem: No.2678 (#1087)
1 parent 145aa05 commit 9bead11

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

solution/2600-2699/2678.Number of Senior Citizens/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,38 @@ func countSeniors(details []string) (ans int) {
120120
}
121121
```
122122

123+
### **Rust**
124+
125+
```rust
126+
impl Solution {
127+
pub fn count_seniors(details: Vec<String>) -> i32 {
128+
let mut ans = 0;
129+
130+
for s in details.iter() {
131+
if let Ok(age) = s[11..13].parse::<i32>() {
132+
if age > 60 {
133+
ans += 1;
134+
}
135+
}
136+
}
137+
138+
ans
139+
}
140+
}
141+
```
142+
143+
```rust
144+
impl Solution {
145+
pub fn count_seniors(details: Vec<String>) -> i32 {
146+
details
147+
.iter()
148+
.filter_map(|s| s[11..13].parse::<i32>().ok())
149+
.filter(|&age| age > 60)
150+
.count() as i32
151+
}
152+
}
153+
```
154+
123155
### **...**
124156

125157
```

solution/2600-2699/2678.Number of Senior Citizens/README_EN.md

+32
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,38 @@ func countSeniors(details []string) (ans int) {
102102
}
103103
```
104104

105+
### **Rust**
106+
107+
```rust
108+
impl Solution {
109+
pub fn count_seniors(details: Vec<String>) -> i32 {
110+
let mut ans = 0;
111+
112+
for s in details.iter() {
113+
if let Ok(age) = s[11..13].parse::<i32>() {
114+
if age > 60 {
115+
ans += 1;
116+
}
117+
}
118+
}
119+
120+
ans
121+
}
122+
}
123+
```
124+
125+
```rust
126+
impl Solution {
127+
pub fn count_seniors(details: Vec<String>) -> i32 {
128+
details
129+
.iter()
130+
.filter_map(|s| s[11..13].parse::<i32>().ok())
131+
.filter(|&age| age > 60)
132+
.count() as i32
133+
}
134+
}
135+
```
136+
105137
### **...**
106138

107139
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn count_seniors(details: Vec<String>) -> i32 {
3+
let mut ans = 0;
4+
5+
for s in details.iter() {
6+
if let Ok(age) = s[11..13].parse::<i32>() {
7+
if age > 60 {
8+
ans += 1;
9+
}
10+
}
11+
}
12+
13+
ans
14+
}
15+
}

0 commit comments

Comments
 (0)