Skip to content

Commit eb34de1

Browse files
authored
feat: add rust solution to lc problem: No.0274 (#1603)
1 parent c6976ff commit eb34de1

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

solution/0200-0299/0274.H-Index/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,30 @@ public:
227227
};
228228
```
229229
230+
### **Rust**
231+
232+
```rust
233+
impl Solution {
234+
#[allow(dead_code)]
235+
pub fn h_index(citations: Vec<i32>) -> i32 {
236+
let mut citations = citations;
237+
citations.sort_by(|&lhs, &rhs| {
238+
rhs.cmp(&lhs)
239+
});
240+
241+
let n = citations.len();
242+
243+
for i in (1..=n).rev() {
244+
if citations[i - 1] >= i as i32 {
245+
return i as i32;
246+
}
247+
}
248+
249+
0
250+
}
251+
}
252+
```
253+
230254
### **Go**
231255

232256
```go

solution/0200-0299/0274.H-Index/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,30 @@ public:
218218
};
219219
```
220220
221+
### **Rust**
222+
223+
```rust
224+
impl Solution {
225+
#[allow(dead_code)]
226+
pub fn h_index(citations: Vec<i32>) -> i32 {
227+
let mut citations = citations;
228+
citations.sort_by(|&lhs, &rhs| {
229+
rhs.cmp(&lhs)
230+
});
231+
232+
let n = citations.len();
233+
234+
for i in (1..=n).rev() {
235+
if citations[i - 1] >= i as i32 {
236+
return i as i32;
237+
}
238+
}
239+
240+
0
241+
}
242+
}
243+
```
244+
221245
### **Go**
222246

223247
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn h_index(citations: Vec<i32>) -> i32 {
4+
let mut citations = citations;
5+
citations.sort_by(|&lhs, &rhs| {
6+
rhs.cmp(&lhs)
7+
});
8+
9+
let n = citations.len();
10+
11+
for i in (1..=n).rev() {
12+
if citations[i - 1] >= i as i32 {
13+
return i as i32;
14+
}
15+
}
16+
17+
0
18+
}
19+
}

0 commit comments

Comments
 (0)