-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_0179_largest_number.rs
More file actions
28 lines (26 loc) · 896 Bytes
/
_0179_largest_number.rs
File metadata and controls
28 lines (26 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct Solution;
impl Solution {
pub fn largest_number(nums: Vec<i32>) -> String {
let mut strings: Vec<String> = nums.iter().map(|n| n.to_string()).collect();
strings.sort_unstable_by(|a, b| (b.clone() + &a.clone()).cmp(&(a.clone() + &b.clone())));
let mut result = "".to_string();
if strings[0] == "0" {
return "0".to_string();
}
for s in strings {
result += &s.clone();
}
result
}
}
#[test]
fn test() {
assert_eq!(Solution::largest_number(vec![10, 2]), "210".to_string());
assert_eq!(
Solution::largest_number(vec![3, 30, 34, 5, 9]),
"9534330".to_string()
);
assert_eq!(Solution::largest_number(vec![1]), "1".to_string());
assert_eq!(Solution::largest_number(vec![10]), "10".to_string());
assert_eq!(Solution::largest_number(vec![0]), "0".to_string());
}