[LeetCode179]Largest Number

题目:

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-] == ) return "";
string s = "";
for(int i = nums.Length - ; i >= ; i--)
{
s += nums[i].ToString();
}
return s;
} public int CompareByTopDigit(int a, int b)
{
return (a + "" + b).CompareTo(b + "" + a);
}
}
上一篇:Mysql修改设置root密码的命令及方法


下一篇:Timeout expired超时时间已到. 达到了最大池大小 错误及Max Pool Size设置