Title: Enter an array of positive integers, and arrange all the numbers in the array into a number, and print one of all the numbers that can be stitched together. For example, the input array {3,32,321} prints out the smallest number 321323 that these 3 numbers can be ranked.
The most straightforward way to do this is to figure out all the numbers in the array, then stitch each arrangement together, and finally find the smallest of the numbers that are arranged. The array of permutations and the questions 28 are very similar. Depending on the permutation, the n numbers have a total of n! permutations, so let's look at a faster algorithm.
This problem actually wants us to find a collation, which is sorted by this rule and can be ranked as the smallest number. To determine the order of the rules, compare two numbers, that is, give two numbers m and N, we need to determine a rule that the M and n which should be in front, rather than just compare the value of these two numbers which is larger.
According to the requirements of the topic, two numbers m and n can be spliced to be called the numbers MN and NM. If mn<nm, then we should print out MN, that is, M should be shot in front of N, we define m less than n at this time, and conversely, if nm<mn, we define n less than M. If Mn=nm,m equals N.
Next consider how to stitch the numbers, that is, to the number of words m and N, how to get the numbers MN and nm and compare their size. It is not difficult to calculate directly with numerical values, but a potential problem is that both M and N are within the range of int's expression, and the numbers MN and nm that are put together are likely to overflow with int, so this is still an invisible large number problem.
A very intuitive way to solve a large number of problems is to convert numbers into strings. In addition, because the number M and N are spliced together to get MN and nm, their digits are certainly the same, so comparing their size only needs to follow the string size of the comparison rules.
Based on this idea, we implement the code:
/** * Line up the array to the smallest number * Enter a positive integer array, combine all the numbers in the array into a number, * print the smallest of all the numbers that can be stitched together */package swordforoffer;/** * @author Jinshuangqi * August 9, 2015 */public class E33sortarrayforminnumber {public void printmin (int[] arr) {int[] clone = Arr.clone ();p Rintminnum ber (clone,0,clone.length-1); for (int i:clone) System.out.print (i);} Core + fast row public void Printminnumber (int[] Arr,int left,int right) {if (left < right) {int main_number = Arr[right];int smal L_cur = left;for (int j = left;j<right;j++) {if (Issmall (string.valueof (Arr[j]), string.valueof (Main_number))) {int temp = arr[j];arr[j] = arr[small_cur];arr[small_cur] = temp;small_cur++;}} arr[right]= Arr[small_cur];arr[small_cur] = Main_number;printminnumber (arr,0,small_cur-1);p Rintminnumber (arr, small_cur+1,right);}} public boolean Issmall (String m,string N) {string left = M+n; String right = N+m;boolean result = false;for (int i = 0;i<left.length (); i++) {if (Left.charat (i) <right.charat (i)) Return True;elseif (Left.charat (i) >right.charat (i) return false;} ReturnResult;} public static void Main (string[] args) {int arr[] = {3,32,321}; E33sortarrayforminnumber test = new E33sortarrayforminnumber (); test.printmin (arr);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The sword refers to the offer surface question (Java Edition): The array to the smallest number