LeetCode 179 Largest Number 把数组排成最大的数

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.

 1 class Solution {
 2 public:
 3     string largestNumber(vector<int>& nums) {
 4         vector<string> arr;
 5         for(int n:nums)
 6             arr.push_back(to_string(n));
 7         sort(arr.begin(),arr.end(),[](string &str1,string &str2){return str1+str2>str2+str1;});
 8         string res;
 9         for(string s:arr)
10             res+=s;
11         if(res[0]=='0')
12             return "0";
13         return res;
14     }
15 };

 

posted on 2018-03-09 20:32  lina2014  阅读(135)  评论(0编辑  收藏  举报

导航