Offer to do to prove safety issues notes (5) arranged in an array of the smallest number

When the report submitted to the code, "please change class called Main" recommendation reset, or the correct code will be reported to the array bounds errors and the like. As to why I will not get to know, but the question becomes just started doing just fine the way after the reset.

References:
https://www.nowcoder.com/questionTerminal/8fecd3f8ba334add803bf2a06af1b993?f=discussion https://blog.csdn.net/fanzitao/article/details/7895344
https://www.cnblogs.com/learnapi/p /9003112.html

Title Description

Enter a positive integer array, the array of all the numbers arranged in a number spliced ​​together, the splice can print out all numbers smallest one. 3,32,321 input array} {e.g., print the minimum number of three numbers can be arranged to 321,323.

The existence of two ways after seeing the disintegration of the cattle off the answer online

Method a: integer converted into ArrayList, custom comparison mode sorting with Collections.sort.

Collections.sort There are two types:

  1. For these Integer String or Comparable interface classes has been achieved, the method can be used directly passed Collections.sort default list of parameters to implement (positive sequence) sort.
  2. For custom type, or a custom ordering can be achieved by a method compareTo Comparable interface.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Solution {
    public String PrintMinNumber(int [] numbers) {
     StringBuilder s =new StringBuilder("");
        ArrayList<Integer> inlist=new ArrayList<Integer>();
        for (int i=0;i<numbers.length;i++){
            inlist.add(numbers[i]);
        }
        Collections.sort(inlist,new Comparator<Integer>(){
            public int compare(Integer s1,Integer s2){
                String str1=s1+""+s2;
                String str2=s2+""+s1;
                return str1.compareTo(str2);
            }
        });
        for(int i:inlist){
        s.append(i);}
        String a=s.toString();
        return a;//return String a=s.toString();是错误的写法
    }
}

Comparison of the sequence of small integers, can be two integers a1, a2 spliced ​​together in a different order compared a1 + a2, a2 ​​+ a1 two small integers which, when a1 + a2 spliced ​​into integer put a1: Method two placed in front of a2. According to this idea over the entire array row sequence, splicing together the entire array will be the smallest integer integer.

Key: sorting; compare the sizes after splicing two integers, then the sequence returns small value splicing

import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
    public static String PrintMinNumber(int [] numbers) {//调用方法,遍历排序后的数组拼接字符串返回结果。
        quicksort(numbers,0,numbers.length-1);
        int i=0;
        String str="";
        while(i<numbers.length)
            str+=numbers[i++];
        return str;
    }
    public static void quicksort(int []arr,int begin,int end){//快速排序
        if(begin>=end)
            return;//
        int index=sort(arr,begin,end);
        quicksort(arr,begin,index-1);
        quicksort(arr,index+1,end);
    }
    public static int sort(int[]arr,int start,int end){//快速排序
        int temp=arr[start];
        int i=start;
        int j=end;
        while(i!=j){
            while(i<j&&!judge(arr,j,temp))
                j--;
            while(i<j&&judge(arr,i,temp))
                i++;
            if(i<j){
                int tem=arr[i];
                arr[i]=arr[j];
                arr[j]=tem;
            }
        }
        arr[start]=arr[i];
        arr[i]=temp;
        return i;
    }
    public static boolean judge(int []arr,int i,int temp){//把两个整数按不同顺序拼接起来比较大小,采用拼接后整数更小的拼接顺序。
        Double a=Double.parseDouble(arr[i]+""+temp);
        Double b=Double.parseDouble(temp+""+arr[i]);//
        if(a<=b)return true;
        else return false;
    }
}

Guess you like

Origin www.cnblogs.com/dongmm031/p/12168890.html