Short 4ms C++ solution using sort


  • 1
    M
    bool compare(long a, long b){
        int e1=10,e2=10;
        while (e1 <= a)e1*=10;
        while (e2 <= b)e2*=10;
        return a * e2 + b > a + b * e1;
    }
    
    
    class Solution {
    public:
        string largestNumber(vector<int>& nums) {
            sort(nums.begin(), nums.end(), compare);
            string res = "";
            for (int i = 0; i < nums.size(); ++i) {
                if(res == "0" && nums[i] == 0)continue;
                res += to_string(nums[i]);
            }
            return res;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.