-
题目: Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input: [10,2] Output: "210"
Example 2:
Input: [3,30,34,5,9] Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer.
-
解法:
static bool myCompare(int a, int b) { //注意:必须为static的function.
string stra = to_string(a);
string strb = to_string(b);
return (stra+strb) > (strb+stra);
}
string largestNumber(vector<int>& nums) {
sort(nums.begin(),nums.end(),myCompare);
if(nums[0] == 0)
return "0";
string ret;
for(int i=0;i<nums.size();i++) {
ret += to_string(nums[i]);
}
return ret;
}