Created
March 2, 2015 05:31
-
-
Save dmnugent80/2f744066278f96a2c343 to your computer and use it in GitHub Desktop.
Arrange Largest Number into String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public String largestNumber(int[] num) { | |
String[] arr = new String[num.length]; | |
for (int i = 0; i < num.length; i++){ | |
arr[i] = Integer.toString(num[i]); | |
} | |
Comparator<String> comp = new Comparator<String>(){ | |
@Override | |
public int compare(String str1, String str2){ | |
String s1 = str1+str2; | |
String s2 = str2+str1; | |
return s2.compareTo(s1); | |
} | |
}; | |
Arrays.sort(arr, comp); | |
if (arr[0].charAt(0) == '0') return "0"; | |
StringBuffer buff= new StringBuffer(); | |
for (int i = 0; i < arr.length; i++){ | |
buff.append(arr[i]); | |
} | |
return buff.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment