码迷,mamicode.com
首页 > 其他好文 > 详细

179. Largest Number

时间:2016-10-08 16:30:15      阅读:115      评论: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.

思路:将array排序,按照相互之间加和大者在前的排在前面为标准.

public class Solution {
    public String largestNumber(int[] nums) {
        
        String[] res=new String[nums.length];
        
        for(int i=0;i<nums.length;i++)
        {
            res[i]=String.valueOf(nums[i]);
        }   
            Arrays.sort(res,(str1,str2)->(str2+str1).compareTo(str1+str2));
        if(res[0].equals("0"))
        {
            return "0";
        }

        StringBuilder sb=new StringBuilder();
        
        for(String check:res)
        {
            sb.append(check);
        }
        return sb.toString();
    }
}

 

 
 

179. Largest Number

标签:

原文地址:http://www.cnblogs.com/Machelsky/p/5938885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!