首页 > 其他 > 详细

[LeetCode179]Largest Number

时间:2015-12-22 00:57:32      阅读:288      评论:0      收藏:0      [点我收藏+]

题目:

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);
    }
}

 

[LeetCode179]Largest Number

原文:http://www.cnblogs.com/zhangbaochong/p/5065263.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!