MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / find_relative_ranks

Function find_relative_ranks

506-relative-ranks.rs:1–22  ·  view source on GitHub ↗
(score: Vec<i32>)

Source from the content-addressed store, hash-verified

1pub fn find_relative_ranks(score: Vec<i32>) -> Vec<String> {
2 let mut sorted_score = score.clone();
3 sorted_score.sort_by(|a, b|b.cmp(a));
4 use std::collections::HashMap;
5 let mut mmp = HashMap::new();
6 for (i, s) in sorted_score.iter().enumerate() {
7 let v: String;
8 let j = i + 1;
9 if j == 1 {
10 v = "Gold Medal".to_string();
11 } else if j == 2 {
12 v = "Silver Medal".to_string();
13 } else if j == 3 {
14 v = "Bronze Medal".to_string();
15 } else {
16 v = j.to_string();
17 }
18 mmp.insert(s, v);
19 }
20 let res: Vec<String> = score.into_iter().map(|e|mmp[&e].clone()).collect();
21 res
22}
23
24fn main() {
25 let score = vec![5,4,3,2,1];

Callers

nothing calls this directly

Calls 1

insertMethod · 0.80

Tested by

no test coverage detected