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();
}
}
SmartFingers
@SmartFingers
Posts made by SmartFingers

Not fast, but may the most readable java solution.

RE: one line java solution
You may fail your interview if you just use Math library.

RE: Why "0" not counted?
But 6009 should be counted. Test case does't cover such kind of numbers

RE: Java beat 100%!!! use prime number
said in Java beat 100%!!! use prime number:
int[] prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103};//最多10609个z
List<List<String>> res = new ArrayList<>(); HashMap<Integer, Integer> map = new HashMap<>(); for (String s : strs) { int key = 1; for (char c : s.toCharArray()) { key *= prime[c  'a']; } List<String> t; if (map.containsKey(key)) { t = res.get(map.get(key)); } else { t = new ArrayList<>(); res.add(t); map.put(key, res.size()  1); } t.add(s); } return res;
The idea is clear: we need to generate a unique key for each group of Strings. A product of several primes can only have one combination of factors, which makes the product a unique key for the Strings consisted with these certain chars.

RE: Fast, easy Java code, with explanation!
I am not sure, but I guess after you add the current list to result, you still need to use that list, so if you directly add it to result, when you change list, the data in result may also be changed since they refer to the same object.

What is difference between backtracking and depth first search?
I know backtracking is one of depth first search, but I notice the questions under backtracking tag are quite different from DFS. After search google, I know backtracking does not keep the entire tree structure while DFS does. Could you please give me more explanation? Prefer examples. Thank you very much!

Fast, backtracking, easy to understand, with explanations!
public class Solution { public List<String> letterCombinations(String digits) { List<String> result = new ArrayList<String>(); String[][] map={{},{},{"a","c","b"},{"d","e","f"},{"g","h","i"},{"j","k","l"},{"m","n","o"},{"p","q","r","s"},,{"t","u","v"},{"w","x","y","z"}}; String single=""; if(digits == null  digits.length() == 0){ //corner case; return result; } helper(result, single, digits, map,0); //go into recursive return result; } private void helper(List<String> result, String single, String digits, String[][] map,int start){ if (start >= digits.length()){ // go out condition result.add(single); return; } int index = digits.charAt(start)'0'; String[] current = map[index]; //get letter collection of current digit for(int i = 0; i < current.length; i++){ single = single + current[i]; //add one letter to current prefix helper(result, single, digits, map, start + 1); //go recursive single=single.substring(0,single.length()1); //remove the last digit, prepare to change to another letter } }
}