c++ solution


  • 0
    B
    
    class Solution {
    public:
        string largestNumber(vector<int>& nums) {
            sort(nums.begin(), nums.end(), 
                [](const int &a, const int&b){
                    return to_string(a)+to_string(b) > to_string(b)+to_string(a);
                }
            );
            
            string result="";
            
            for(int n:nums) result+= to_string(n);
            //in case "00"
            while(result.size()>1 && result[0]=='0' && result[1]=='0') 
                result.erase(result.begin());
            
            return result;
        }
    };
    
    

Log in to reply
 

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