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 is very large, so you need to return a string instead of an integer.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Give a random ordinal group the maximum number of components that it is required to sequence
Basic requirements or sorting is only compared to the size of the basis of different, such as 412<41 but 415>41 comparison is based on who is placed in the back of the larger number so as long as the N1 and N2 are converted to string respectively before and after the comparison of the size of the two composition string returned results The sorting method takes a quick sort at the beginning of the sort () function part of the recursion when I wrongly write for mid causes the dead loop to take a long time to discover the code as follows:
public class Solution {public static string Largestnumber (int[] num) {string ss= ""; if (num.length==0) {return SS;} Sort (num, 0, num.length-1), if (num[num.length-1]==0) return "0"; for (int i=num.length-1;i>=0;i--) {ss+= String.valueof (Num[i]);} return SS;} public static void Sort (int[] num,int left,int right) {if (right-left<10) {for (int. i=left;i<right;i++) {for (int j=i; j<=right;j++) {if (bigger (Num[i], num[j]) ==1) {int tmp=num[j];num[j]=num[i];num[i]=tmp;}}}} Else{int mid=mid (num, left, right); int i=left;int j=right-1;for (;;) {while (bigger (num[++i],mid) ==-1) {};while (bigger (num[--j],mid) ==1) {};if (i<j) {swap (num, I, j);} Else{break;}} Swap (num, I, right-1), sort (num, left, i-1); sort (num, i+1,right);}} public static int mid (int[] Num,int left,int right) {int mid= (left+right)/2;if (bigger (Num[left],num[mid]) ==1) {swap (num , left, mid);} if (bigger (Num[mid],num[right]) ==1) {swap (num, right, mid);} if (bigger (Num[left],num[mid]) ==1) {swap (num, left, mid);} Swap (num, mid, right-1); return num[right-1];} Public static void Swap (int[] Num,int i,int j) {int tmp=num[j];num[j]=num[i];num[i]=tmp;} public static int bigger (int num1,int num2) {String ss1=string.valueof (NUM1) +string.valueof (num2);; String ss2=string.valueof (num2) +string.valueof (NUM1); for (int i=0;i<ss1.length (); i++) {if (Ss1.charat (i) > Ss2.charat (i)) {return 1;} Else{if (Ss1.charat (i) <ss2.charat (i) {return-1;}}} return 0;}}
Java-largest number