• 58. 把数组排成最小的数


     思路: https://www.cnblogs.com/lliuye/p/9159152.html

    2 思路描述:

    sort(begin,end,cmp),cmp参数默认升序。

    ⑤vector进行排序
    sort(nums.begin(),nums.end());

    static bool cmp(int &a, int &b) {
        string ab = to_string(a) + to_string(b);
        string ba = to_string(b) + to_string(a);
        return ab < ba;
    }
    string printMinNumber(vector<int>& nums) {
        string res;
        if(nums.empty()) return res;
        sort(nums.begin(),nums.end(),cmp);//对数组元素进行降序排序,构造排序函数cmp
        for(int i = 0;i<nums.size();i++)
        {
            res += to_string(nums[i]);
        }
        return res;
    }

     注意了bool前的static不能少,否则编译通不过

    另一种写法

    class Solution {
    public:
        string PrintMinNumber(vector<int> numbers) {
            int length = numbers.size();
            if(length == 0){
                return "";
            }
            sort(numbers.begin(), numbers.end(), cmp);
            string res;
            for(int i = 0; i < length; i++){
                res += to_string(numbers[i]);
            }
            return res;
        }
    private:
        // 升序排序
        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;
        }
    };
    
    带女朋友搬家新家条件不好,累到女朋友了,让女朋友受苦了,特此明志:每天学习,明年这个时候(20190812)让女朋友住上大房子,永远年轻,永远热泪盈眶,很多人都是这样,他们都把自己当成身在梦中一样,浑浑噩噩地过日子,只有痛苦或爱或危险可以让他们重新感到这个世界的真实。
  • 相关阅读:
    Unity3D性能优化之美术资源制件规范
    Unity3D屏幕自适应
    Unity3D性能优化之内存科普篇
    面向对象设计和特性
    Uinty3D性能优化之贴图科普篇
    Learn OpenGL 概念(一)
    假如博客园也有年度总结报告……
    2019年修图汇总
    Python 小案例实战 —— 简易银行存取款查询系统
    Win10 + Anaconda + Tensorflow-cpu + Pycharm安装教程
  • 原文地址:https://www.cnblogs.com/make-big-money/p/12323294.html
Copyright © 2020-2023  润新知