Share My Concise Solution


  • 1
    A
    class NumberComparator implements Comparator<String> {
            
            @Override
            public int compare(String i1, String i2) {       
                int i=0;
                while (i < i1.length() && i < i2.length()) {  
                	int diff = i1.charAt(i) - i2.charAt(i);
                	if (diff == 0) {
                		i++;
                	} else {
                		return diff;
                	}
                }  
                if (i < i1.length()) {
                	return compare(i1.substring(i), i2);
                } else if (i < i2.length()) {
                	return compare(i1, i2.substring(i));
                } else {
                	return 0;
                }
            } 
        }
        
        public String largestNumber(int[] num) {
        	String[] numNew = new String[num.length];
        	for (int i=0; i<num.length; i++) numNew[i] = String.valueOf(num[i]);
        	Comparator<String> comp = new NumberComparator();
            Arrays.sort(numNew, comp);
            StringBuffer strBuf = new StringBuffer();
            for (int i=num.length-1; i>=0; i--) {
                strBuf.append(String.valueOf(numNew[i]));
            }
            return strBuf.charAt(0) == '0' ? "0" : strBuf.toString();
        }

Log in to reply
 

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