Prove safety offer ----- 32, the arranged array of the smallest number

1, 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.

2, analysis

        Of course, this problem can be solved with violence, then the complexity will be higher, where the use of STL sort algorithm can define their own sorting rules, so will the number after number two splice comparison, after relatively complete order add up just fine. In fact, we have a sort an array of custom rules, this sort requires a small digital array put together in front after large on the back.

3, Code

class Solution {
public:
    string PrintMinNumber(vector<int> numbers) {
        if(numbers.size()==0) return "";
        sort(numbers.begin(),numbers.end(),cmp);
        string res;
        for(int i=0;i<numbers.size();++i){
            res+=to_string(numbers[i]);
        }
        return res;
    }
    static bool cmp(int a ,int b){
        string A=to_string(a)+to_string(b);
        string B=to_string(b)+to_string(a);
        return A<B;
    }
};

4, the relevant knowledge points

        Use the sort of. You can refer to this article. ( Https://www.cnblogs.com/houkai/p/3566510.html )

Guess you like

Origin blog.csdn.net/zl6481033/article/details/94207680