Java solution,quickSort.


  • 1
    O
    public String largestNumber(int[] num) {
        if(num==null||num.length==0) return null;
        
        StringBuilder result=new StringBuilder();
        String[] cnum = new String[num.length];
        
        for(int i=0;i<num.length;i++)
        {
            cnum[i] = num[i]+"";
        }
        
        quickSort(cnum,0,cnum.length-1);
        
        for(String str:cnum)
        {
            result.append(str);
        }
        
        if(result.charAt(0) == '0') return "0";
        
        return result.toString();
    }
    
    private void quickSort(String[] num,int l,int r)
    {
        if(l>=r) return;
        int i=l,j=r;
        String x=num[l];
        
        while(i<j)
        {
            while(i<j&&(x+num[j]).compareTo(num[j]+x)>=0)
            j--;
            if(i<j)
            num[i++] = num[j];
            while(i<j&&(x+num[i]).compareTo(num[i]+x)<0)
            i++;
            if(i<j)
            num[j--] = num[i];
        }
        num[i] = x;
        quickSort(num,l,i-1);
        quickSort(num,i+1,r);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.