LargestNumber

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

出处:https://leetcode.com/problems/largest-number/description/

 

大神的解法:

    

class Solution {
    public String largestNumber(int[] nums) {
        if(nums==null || nums.length==0){
            return "";
        }
        int n=nums.length; 
        String [] res=new String[n];
        for(int i=0; i(){
            public int compare(String a, String b){
                String s1=a+b;
                String s2=b+a;
                return s2.compareTo(s1);
            }
        });
        
        if(res[0].charAt(0)=='0'){
            return "0";
        }
        StringBuilder sb=new StringBuilder();
        for(String s : res){
            sb.append(s);
        }
        return sb.toString(); 
    }
}

       自己的解法大体是,利用已有的最大序列(用一个其他符号将每一个数分开),然后将新的数字不断与之前的每一个数字比较,从而检查得到最大的新序列,时间复杂度(n^2),想较自己的解法,上面的代码中用了sort()进行了排列,减少了时间复杂度。

你可能感兴趣的:(LeetCode)