LeetCode---(197)Largest Number

来源:互联网 发布:上海兄弟连java学费 编辑:程序博客网 时间:2024/05/03 11:50

Given a list of non negative integers, arrange them such that they form the largest number.

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

class Solution {public:    static bool cmp(string a,string b)    {        string c1=a+b;        string c2=b+a;        return c1>c2;    }    string largestNumber(vector<int>& nums) {        if(nums.size()==0)            return "";        vector<string> num_str;        for(int i=0;i<nums.size();i++)            num_str.push_back(to_string(nums[i]));        sort(num_str.begin(),num_str.end(),cmp);                string res="";        for (int i = 0; i < num_str.size(); i++)              res += num_str[i];          if (res[0] == '0')              return "0";          return res;        }};


0 0