Leetcode 179 Largest Number Solution in c++ | Hindi Coding Community

0


Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.

Since the result may be very large, so you need to return a string instead of an integer.


Example 1:


Input: nums = [10,2]

Output: "210"



class Solution {
public:
static bool comp(string& a, string& b)
{
    return a+b>b+a;
}
    string largestNumber(vector<int>& nums) {
        int n=nums.size();
        vector<string> v;
        for(int i:nums)
        v.push_back(to_string(i));
        sort(v.begin(), v.end(), comp);
        string s="";
        for(auto a:v)
        s+=a;
        int i=0;
        while(i<s.size()-1 && s[i]=='0')
        i++;
        return s.substr(i, s.size());
    }
};

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !