Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
Created March 2, 2015 05:31
Show Gist options
  • Save dmnugent80/2f744066278f96a2c343 to your computer and use it in GitHub Desktop.
Save dmnugent80/2f744066278f96a2c343 to your computer and use it in GitHub Desktop.
Arrange Largest Number into String
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