码迷,mamicode.com
首页 > 其他好文 > 详细

179. Largest Number

时间:2018-10-30 10:20:13      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:number   instead   form   integer   amp   res   put   time   ber   

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.

 
AC code:
class Solution {
public:
    string largestNumber(vector<int>& nums) {
        vector<string> temp;
        string ans = "";
        for (auto num : nums) {
            temp.push_back(to_string(num));
        }
        sort(temp.begin(), temp.end(), [](string& s1, string& s2) {return s1+s2 > s2+s1;});
        for (auto i : temp) {
            ans += i;
        }
        while (ans[0] == ‘0‘ && ans.length() > 1)
            ans.erase(0, 1);
        return ans;
    }
};

Runtime: 8 ms, faster than 68.77% of C++ online submissions for Largest Number.

 

179. Largest Number

标签:number   instead   form   integer   amp   res   put   time   ber   

原文地址:https://www.cnblogs.com/ruruozhenhao/p/9875035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!