Share a short code in c++


  • 41
    L
    class Solution {
    public:
        string largestNumber(vector<int> &num) {
            sort(num.begin(), num.end(), [](int a, int b){
                return to_string(a)+to_string(b) > to_string(b)+to_string(a);
            });
            string ans;
            for(int i=0; i<num.size(); i++){
                ans += to_string(num[i]);
            }
            return ans[0]=='0' ? "0" : ans;
        }
    };

  • 0
    This post is deleted!

  • 0
    C

    OMG, I just implemented the string comparison myself. LOL


  • 0
    C

    Great solution.
    Would it be faster if using a vector<string> beforehand? So to_string's are used less.


Log in to reply
 

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