题目:

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

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

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

思路:将数字按照最高位的大小排序,然后连接成字符串即可

代码:

public class Solution {
    public string LargestNumber(int[] nums) {
        Array.Sort(nums,CompareByTopDigit);
        if(nums[nums.Length-1] == 0) return "0";
        string s = "";
        for(int i = nums.Length - 1; i >= 0; i--)
        {
            s += nums[i].ToString();
        }
        return s;
    }
    
    public int CompareByTopDigit(int a, int b)
    {
        return (a + "" + b).CompareTo(b + "" + a);
    }
}

 

相关文章:

  • 2021-08-07
  • 2021-12-20
  • 2021-09-22
  • 2021-07-05
  • 2022-03-03
  • 2021-08-22
猜你喜欢
  • 2022-12-23
  • 2021-09-11
  • 2022-01-24
  • 2021-11-15
  • 2021-09-13
相关资源
相似解决方案