General solution


  • 0
    J
    class Solution {
    bool compare(int a,int b)
    {
        stringstream ss,sss;
        string p1,p2;
        ss<<a;
        ss>>p1;
        sss<<b;
        sss>>p2;
        return p1+p2>=p2+p1;
    }
    public:
    string largestNumber(vector<int>& nums) {
        if(nums.size()==0)
            return "";
        int start=1;
        while(start<=nums.size()-1)
        {
            int val=nums[start];
            int it=start-1;
            while(it>=0&&compare(val,nums[it]))
            {
                nums[it+1]=nums[it];
                it--;
            }
            nums[it+1]=val;
            start++;
        }
        string res="";
        for(int i=0;i!=nums.size();i++)
        {
            stringstream ss;
            string p;
            ss<<nums[i];
            ss>>p;
            res=res+p;
        }
        if(res[0]=='0')
            return "0";
        return res;
    }};

Log in to reply
 

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