ACWING58. The minimum number of arrays arranged (the offer to prove safety)

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.

E.g. input array [3, 32, 321], print the minimum number of three digits can be arranged to 321,323.

Sample
input: [3, 32, 321]

Output: 321 323
Note: The format for the digital output string.

class Solution {
public:
    static bool cmp(string a,string b) {
        return a + b < b + a;
    }
    
    string printMinNumber(vector<int>& nums) {
        int n = nums.size();
        vector<string>a;
        for(int i = 0;i < n;i++) {
            a.push_back(to_string(nums[i]));
        }
        sort(a.begin(),a.end(),cmp);
        string ans;
        for(int i = 0;i < n;i++) {
            ans += a[i];
        }
        return ans;
    }
};
Published 844 original articles · won praise 28 · views 40000 +

Guess you like

Origin blog.csdn.net/tomjobs/article/details/104970902