Leetcode: Largest Number, leetcodelargest
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be very large, so you need to return a string instead of an integer.
Let's take a look at this question. The largest number in the first place should be in the front, for example, 9 should be in the front, and 5 should be in the second place. So how should we deal with 3, 30, and 34?
The first thought was to compare a person to a person, for example, 30 to 34. But for example, how can we compare 3 to 30 digits? At first I thought 3 was equivalent to 33, so the last one of 34 and 30 is better than 3.
But in this case 824 and 8247, according to my understanding, 824 <8247, but in fact, because 8248247> 8247824.
Only then can we find that the cycle is required. After the ratio of the single-digit ratio of 824 is reached, we need to jump back to 8; 8247 is also true. After the ratio is 7, we need to jump back to 8.
So there are the following comparison methods: (slightly stupid)
1 public class Solution { 2 public String largestNumber(int[] num) { 3 StringBuffer res = new StringBuffer(); 4 String[] str = new String[num.length]; 5 for (int i=0; i<num.length; i++) { 6 str[i] = Integer.toString(num[i]); 7 } 8 Comparator<String> comp = new Comparator<String>() { 9 public int compare(String t1, String t2) {10 int i = 0;11 int j = 0;12 while (i != t1.length()-1 || j != t2.length()-1) {13 if (t1.charAt(i) != t2.charAt(j)) {14 return t1.charAt(i) - t2.charAt(j);15 }16 else {17 i = (i + 1) % t1.length();18 j = (j + 1) % t2.length();19 }20 }21 return t1.charAt(i)==t2.charAt(j)? 0 : t1.charAt(i)-t2.charAt(j);22 }23 };24 Arrays.sort(str, comp);25 for (int j=str.length-1; j>=0; j--) {26 res.append(str[j]);27 }28 while(res.length() > 1 && res.charAt(0) == '0') { // delete front 029 res.deleteCharAt(0);30 }31 return res.toString();32 }33 }
Clever method: in fact, why should we compare them one by one? in the most direct way, who is the most advanced:
Another idea is to compare the priority of two numbers in the final result. Why not compare the result size of different combinations directly?
For example, to compare the positions 3 and 34, we can compare the sizes of 334 and 343, while 343 is bigger than 334, so 34 should be in front.
In this way, you can sort the entire array by comparing two numbers. Then we can splice the sorted numbers together.
First, convert all int values to string array. Then, write a comparator to determine the size of AB ba and sort a and B.
Then connect all the connections. Remember, the big ones are behind and the connection starts from behind. Finally, remove the preceding 0;
1 public class Solution { 2 public String largestNumber(int[] num) { 3 StringBuffer res = new StringBuffer(); 4 String[] str = new String[num.length]; 5 for (int i=0; i<num.length; i++) { 6 str[i] = Integer.toString(num[i]); 7 } 8 Comparator<String> comp = new Comparator<String>() { 9 public int compare(String t1, String t2) {10 String t12 = t1 + t2;11 String t21 = t2 + t1;12 for (int i=0; i<t1.length()+t2.length(); i++) {13 if (t12.charAt(i) != t21.charAt(i)) {14 return t12.charAt(i) - t21.charAt(i);15 }16 }17 return 0;18 }19 };20 Arrays.sort(str, comp);21 for (int j=str.length-1; j>=0; j--) {22 res.append(str[j]);23 }24 while(res.length() > 1 && res.charAt(0) == '0') { // delete front 025 res.deleteCharAt(0);26 }27 return res.toString();28 }29 }
Others' method reference: similar ideas, mainly referring to his comparator Writing Method
1 public class Solution { 2 public String largestNumber(int[] num) { 3 if(num.length==0) return null; 4 5 String[] strs = new String[num.length]; 6 for(int i=0; i<num.length; i++){ 7 strs[i] = Integer.toString(num[i]); 8 } 9 10 Arrays.sort(strs, new myComparator());11 StringBuilder sb = new StringBuilder();12 for(int i=strs.length-1; i>=0; i--){13 sb.append(strs[i]);14 }15 16 int i=0; 17 while(i<strs.length && sb.charAt(i) == '0'){18 i++;19 }20 21 if(i==strs.length) return "0";22 return sb.toString().substring(i);23 }24 25 public class myComparator implements Comparator<String>{26 public int compare(String a, String b){27 String ab = a+b;28 String ba = b+a;29 return Integer.parseInt(ab)-Integer.parseInt(ba);30 }31 }32 }