Skip to content

Commit e76b651

Browse files
committed
Solve #274
1 parent 45b431d commit e76b651

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,5 @@ mod n0260_single_number_iii;
211211
mod n0263_ugly_number;
212212
mod n0264_ugly_number_ii;
213213
mod n0268_missing_number;
214+
mod n0273_integer_to_english_words;
215+
mod n0274_h_index;

src/n0273_integer_to_english_words.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* [273] Integer to English Words
3+
*
4+
* Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2^31 - 1.
5+
*
6+
* Example 1:
7+
*
8+
*
9+
* Input: 123
10+
* Output: "One Hundred Twenty Three"
11+
*
12+
*
13+
* Example 2:
14+
*
15+
*
16+
* Input: 12345
17+
* Output: "Twelve Thousand Three Hundred Forty Five"
18+
*
19+
* Example 3:
20+
*
21+
*
22+
* Input: 1234567
23+
* Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
24+
*
25+
*
26+
* Example 4:
27+
*
28+
*
29+
* Input: 1234567891
30+
* Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
31+
*
32+
*
33+
*/
34+
pub struct Solution {}
35+
36+
// submission codes start here
37+
38+
// static digits: Vec<&str> = vec!["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"];
39+
// static xteens: Vec<&str> = vec!["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventenn", "Eighteen", "Nineteen"];
40+
// static xtys: Vec<&str> = vec!["Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
41+
// static hunderd: &str = "Hunderd";
42+
// static thousands: Vec<&str> = vec!["Thousand", "Million", "Billion"];
43+
44+
impl Solution {
45+
pub fn number_to_words(num: i32) -> String {
46+
"".to_owned()
47+
}
48+
}
49+
50+
// submission codes end
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use super::*;
55+
56+
#[test]
57+
fn test_273() {}
58+
}

src/n0274_h_index.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* [274] H-Index
3+
*
4+
* Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
5+
*
6+
* According to the <a href="https://en.wikipedia.org/wiki/H-index" target="_blank">definition of h-index on Wikipedia</a>: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N - h papers have no more than h citations each."
7+
*
8+
* Example:
9+
*
10+
*
11+
* Input: citations = [3,0,6,1,5]
12+
* Output: 3
13+
* Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had
14+
* received 3, 0, 6, 1, 5 citations respectively.
15+
* Since the researcher has 3 papers with at least 3 citations each and the remaining
16+
* two with no more than 3 citations each, her h-index is 3.
17+
*
18+
* Note: If there are several possible values for h, the maximum one is taken as the h-index.
19+
*
20+
*/
21+
pub struct Solution {}
22+
23+
// submission codes start here
24+
25+
impl Solution {
26+
pub fn h_index(mut citations: Vec<i32>) -> i32 {
27+
citations.sort_unstable();
28+
let mut hidx = 0;
29+
for &v in citations.iter().rev() {
30+
if v >= hidx + 1 {
31+
hidx += 1;
32+
} else {
33+
break;
34+
}
35+
}
36+
hidx
37+
}
38+
}
39+
40+
// submission codes end
41+
42+
#[cfg(test)]
43+
mod tests {
44+
use super::*;
45+
46+
#[test]
47+
fn test_274() {
48+
assert_eq!(Solution::h_index(vec![3,0,6,1,5]), 3);
49+
}
50+
}

0 commit comments

Comments
 (0)