跳至主要内容

179. 最大数

class Solution {
public:
    string largestNumber(vector<int>& nums) {
        std::sort(nums.begin(), nums.end(), [](int a, int b)
        {
            int a1 = a;
            int n = 1;
            long a2 = 10;
            while (a1 >= 10)
            {
                a1 /= 10;
                a2 *= 10;
                n++;
            }

            int b1 = b;
            int m = 1;
            long b2 = 10;
            while (b1 >= 10)
            {
                b1 /= 10;
                b2 *= 10;
                m++;
            }

            return a*b2+b>b*a2+a;
        });
       
        if (nums[0] == 0)
        {
            return "0";
        }
        string ret = "";
        for (int i = 0; i < nums.size(); i++)
        {
            char buff[100];
            sprintf(buff, "%d", nums[i]);
            ret += buff;
        }
        return ret;
    }
};

评论

此博客中的热门博文

源码编译LLVM

  cmake -S llvm -B build -G "Unix Makefiles" -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt;libc;openmp" -DCMAKE_BUILD_TYPE=Release   cd build/   make -j8