179. Largest Number

原创 2018年04月17日 13:01:51

问题描述

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.

题目链接:


思路分析

给一数组,将它合成一个最大的数,以string形式返回。

关键在于要将首位最大的数放在前面,这就需要自己设计一个sort的comparator,sort的条件是对比两个string相加的值,将大的放在前面,这就是一个冒泡排序,只不过STL给我们简化了。

然后将结果res string的前面不合理的0都去掉就可以了。

代码

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

时间复杂度:O(n)
空间复杂度:O(n)


反思

关键在于sort的comparator的设计,在函数内部inline重载。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BigFatSheep/article/details/79973272

实战COM(07)----COM+的特性之建立者字符串

本文欢迎转载,唯请注明出处及作者 blackcolor@263.net---------------------------------------------------              ...
  • blackcolor
  • blackcolor
  • 2001-08-03 10:12:00
  • 780

179 Largest Number

题目链接:https://leetcode.com/problems/largest-number/题目:Given a list of non negative integers, arrange ...
  • ChilseaSai
  • ChilseaSai
  • 2016-01-31 22:03:04
  • 793

LeetCode(179) Largest Number

题目如下: Given a list of non negative integers, arrange them such that they form the largest number. F...
  • feliciafay
  • feliciafay
  • 2015-01-21 15:58:05
  • 2664

leetcode 179: Largest Number

Largest Number Total Accepted: 2269 Total Submissions: 15366 Given a list of non negative integer...
  • xudli
  • xudli
  • 2015-01-16 04:12:19
  • 4276

[leetcode-179]Largest Number(java)

问题描述: Given a list of non negative integers, arrange them such that they form the largest number.Fo...
  • zdavb
  • zdavb
  • 2015-08-29 00:32:39
  • 753

Leetcode 179. Largest Number (Medium) (java)

Leetcode 179. Largest Number (Medium) (java)
  • Niko_Ke
  • Niko_Ke
  • 2016-09-22 13:58:37
  • 269

[Leetcode] 179. Largest Number @python

题目Given a list of non negative integers, arrange them such that they form the largest number.For exa...
  • qian2729
  • qian2729
  • 2016-02-05 17:00:05
  • 513

LeetCode 179. Largest Number(最大数)

原题网址:https://leetcode.com/problems/largest-number/ Given a list of non negative integers, arrange ...
  • jmspan
  • jmspan
  • 2016-05-25 01:36:20
  • 424

leetcode179-Largest Number(把数组排成最大的数)

问题描述:输入一个正整数数组,把数组里的所有数字拼接起来排成一个数,打印能拼接出的所有数字中最大的一个。例如输入数组{3,32,321},则打印出332321.问题求解:这道题和剑指offer面试题3...
  • will130
  • will130
  • 2016-06-11 11:52:22
  • 1737

[LeetCode]179. Largest Number

Problem DescriptionGiven a list of non negative integers, arrange them such that they form the large...
  • u012468264
  • u012468264
  • 2016-02-05 14:35:41
  • 193
收藏助手
不良信息举报
您举报文章:179. Largest Number
举报原因:
原因补充:

(最多只允许输入30个字)