Skip to content

Latest commit

 

History

History
33 lines (27 loc) · 817 Bytes

File metadata and controls

33 lines (27 loc) · 817 Bytes

179. Largest Number

Given a list of non negative integers, arrange them such that they form the largest number.

Example 1:

Input: [10,2]
Output: "210"

Example 2:

Input: [3,30,34,5,9]
Output: "9534330"

Note: The result may be very large, so you need to return a string instead of an integer.

Solutions (Rust)

1. Sort

impl Solution {
    pub fn largest_number(nums: Vec<i32>) -> String {
        let mut nums = nums.iter().map(|num| num.to_string()).collect::<Vec<_>>();
        nums.sort_unstable_by(|a, b| (b.to_owned() + a).cmp(&(a.to_owned() + b)));

        if nums.len() > 0 && nums[0] == 0.to_string() {
            return 0.to_string();
        }
        nums.concat()
    }
}