Share to simple solution using customized comparator C++


  • 6
    C
    struct less_than_key
    {
        inline bool operator() (int s1, int s2)
        {
            string t1=to_string(s1)+to_string(s2);
            string t2=to_string(s2)+to_string(s1);
            
            return (t1.compare(t2)>0);
        }
    };
    
    class Solution {
    public:
        string largestNumber(vector<int> &num) {
            
            sort(num.begin(),num.end(),less_than_key());
            
            string r="";
            for(int i=0;i<num.size();i++)
            {
                r.append(to_string(num[i]));
            }
            if(r[0]=='0') return "0";
            return r;
        }
    };

Log in to reply
 

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