Not fast, but may the most readable java solution.


  • 0
    S
    public String largestNumber(int[] nums) {
            Integer[] nn = new Integer[nums.length];  // Built an Integer array to implement sort function
            for(int i = 0; i < nums.length; i++){
                nn[i] = nums[i];
            }
            Arrays.sort(nn, new Comparator<Integer>(){
                public int compare(Integer a, Integer b){  //compare which is bigger. 
                    String aa = (a + "") + (b + "");            //If you like, can also convert to long number and compare
                    String bb = (b + "") + (a + "");
                    for(int i = 0; i < aa.length(); i++){
                        if (aa.charAt(i) > bb.charAt(i)){
                            return -1;
                        } else if (aa.charAt(i) < bb.charAt(i)){
                            return 1;
                        }
                    }
                    return 0;
                }
            });
            StringBuilder res = new StringBuilder();
            for(int i : nn){
                res = res.append(i);
            }
            if (nn[0] == 0){                                  //if the biggest one is 0, return "0" directly.
                return "0";
            } else {
                return res.toString();
            }
        }
    

Log in to reply
 

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