From edbe6e86d8e4cb0a6aa4518bf2461e6388e19902 Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Mon, 11 Sep 2023 09:21:15 -0400 Subject: [PATCH] feat: add rust solution to lc problem: No.0274 --- solution/0200-0299/0274.H-Index/README.md | 24 ++++++++++++++++++++ solution/0200-0299/0274.H-Index/README_EN.md | 24 ++++++++++++++++++++ solution/0200-0299/0274.H-Index/Solution.rs | 19 ++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 solution/0200-0299/0274.H-Index/Solution.rs diff --git a/solution/0200-0299/0274.H-Index/README.md b/solution/0200-0299/0274.H-Index/README.md index bedd90e831c00..9b4304efcdf95 100644 --- a/solution/0200-0299/0274.H-Index/README.md +++ b/solution/0200-0299/0274.H-Index/README.md @@ -227,6 +227,30 @@ public: }; ``` +### **Rust** + +```rust +impl Solution { + #[allow(dead_code)] + pub fn h_index(citations: Vec) -> i32 { + let mut citations = citations; + citations.sort_by(|&lhs, &rhs| { + rhs.cmp(&lhs) + }); + + let n = citations.len(); + + for i in (1..=n).rev() { + if citations[i - 1] >= i as i32 { + return i as i32; + } + } + + 0 + } +} +``` + ### **Go** ```go diff --git a/solution/0200-0299/0274.H-Index/README_EN.md b/solution/0200-0299/0274.H-Index/README_EN.md index f2e4c43a6db7c..906d87d6292d0 100644 --- a/solution/0200-0299/0274.H-Index/README_EN.md +++ b/solution/0200-0299/0274.H-Index/README_EN.md @@ -218,6 +218,30 @@ public: }; ``` +### **Rust** + +```rust +impl Solution { + #[allow(dead_code)] + pub fn h_index(citations: Vec) -> i32 { + let mut citations = citations; + citations.sort_by(|&lhs, &rhs| { + rhs.cmp(&lhs) + }); + + let n = citations.len(); + + for i in (1..=n).rev() { + if citations[i - 1] >= i as i32 { + return i as i32; + } + } + + 0 + } +} +``` + ### **Go** ```go diff --git a/solution/0200-0299/0274.H-Index/Solution.rs b/solution/0200-0299/0274.H-Index/Solution.rs new file mode 100644 index 0000000000000..f0f6d4e0d3db3 --- /dev/null +++ b/solution/0200-0299/0274.H-Index/Solution.rs @@ -0,0 +1,19 @@ +impl Solution { + #[allow(dead_code)] + pub fn h_index(citations: Vec) -> i32 { + let mut citations = citations; + citations.sort_by(|&lhs, &rhs| { + rhs.cmp(&lhs) + }); + + let n = citations.len(); + + for i in (1..=n).rev() { + if citations[i - 1] >= i as i32 { + return i as i32; + } + } + + 0 + } +} \ No newline at end of file