Skip to content

Commit 046bec9

Browse files
committed
Added solution of problem 179
1 parent 55fc28f commit 046bec9

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/solutions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub mod s0165_compare_version_numbers;
9696
pub mod s0167_two_sum_ii_input_array_is_sorted;
9797
pub mod s0172_factorial_trailing_zeroes;
9898
pub mod s0173_binary_search_tree_iterator;
99+
pub mod s0179_largest_number;
99100
pub mod s0231_power_of_two;
100101
pub mod s0232_implement_queue_using_stacks;
101102
pub mod s0234_palindrome_linked_list;

src/solutions/s0179_largest_number.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
pub fn largest_number(nums: Vec<i32>) -> String {
2+
let mut num_strs: Vec<String> = nums.into_iter().map(|num| num.to_string()).collect();
3+
num_strs.sort_by(|a, b| {
4+
let order1 = format!("{}{}", a, b);
5+
let order2 = format!("{}{}", b, a);
6+
order2.cmp(&order1)
7+
});
8+
9+
let result = num_strs.join("");
10+
if result.starts_with('0') {
11+
"0".to_string()
12+
} else {
13+
result
14+
}
15+
}
16+
17+
#[cfg(test)]
18+
mod test {
19+
use super::*;
20+
21+
#[test]
22+
fn test_case_1() {
23+
assert_eq!(largest_number(vec![10, 2]), "210");
24+
}
25+
26+
#[test]
27+
fn test_case_2() {
28+
assert_eq!(largest_number(vec![3, 30, 34, 5, 9]), "9534330");
29+
}
30+
}

0 commit comments

Comments
 (0)