您的位置:首页 > 其它

LeetCode---(197)Largest Number

2015-07-26 16:16 459 查看
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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: