首页 > 代码库 > 179. Largest Number

179. 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.

思路:将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