easy understand java solution but slow


  • 0
    L

    class Solution {

    public String largestNumber(int[] nums) {
        String res = "";
        //  bubble sort
        for(int i=0; i<nums.length-1; i++){
            for(int j=0; j<nums.length-i-1; j++){
                if(compare(nums[j],nums[j+1])<0){
                    int tmp = nums[j];
                    nums[j] = nums[j+1];
                    nums[j+1] = tmp;
                }
            }
        }
        if(nums[0]==0)return "0";
        for(int tmp:nums){
            res += tmp;
        }
        return res;
    }
    
    public int compare(int com1,int com2){
        return (com1+""+com2).compareTo(com2+""+com1);
    }
    

    }


Log in to reply
 

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