Java-largest number

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.