剑指offer--32.把数组排成最小的数

用to_string()将整形转化为字符串,对字符串进行比较
------------------------------------------------------------------------------------------
时间限制:1秒 空间限制:32768K 热度指数:213153
本题知识点: 数组

题目描述

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
class Solution {
    public:
        string PrintMinNumber(vector<int> numbers) {
            int len = numbers.size();
            if(len == 0) return "";
            sort(numbers.begin(), numbers.end(), cmp);
            string res;
            for(int i = 0; i < len; 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;
        }
};
原文地址:https://www.cnblogs.com/slothrbk/p/10623828.html