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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

这道题给了我们一个数组,让将其拼接成最大的数,那么根据题目中给的例子来看,主要就是要给数组进行排序,但是排序方法不是普通的升序或者降序,因为9要排在最前面,而9既不是数组中最大的也不是最小的,所以要自定义排序方法。如果不参考网友的解法,博主估计是无法想出来的。这种解法对于两个数字a和b来说,如果将其都转为字符串,如果 ab > ba,则a排在前面,比如9和34,由于 934>349,所以9排在前面,再比如说 30 和3,由于 303<330,所以3排在 30 的前面。按照这种规则对原数组进行排序后,将每个数字转化为字符串再连接起来就是最终结果。代码如下:

class Solution {
public:
string largestNumber(vector<int>& nums) {
string res;
sort(nums.begin(), nums.end(), [](int a, int b) {
return to_string(a) + to_string(b) > to_string(b) + to_string(a);
});
for (int i = ; i < nums.size(); ++i) {
res += to_string(nums[i]);
}
return res[] == '' ? "" : res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/179

参考资料:

https://leetcode.com/problems/largest-number/

https://leetcode.com/problems/largest-number/discuss/53158/My-Java-Solution-to-share

https://leetcode.com/problems/largest-number/discuss/53157/A-simple-C%2B%2B-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Largest Number 最大组合数的更多相关文章

  1. [LeetCode] 179. Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. Example ...

  2. Leetcode Largest Number c++ solution

    Total Accepted: 16020 Total Submissions: 103330     Given a list of non negative integers, arrange t ...

  3. LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用

    Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...

  4. LeetCode——Largest Number

    Description: Given a list of non negative integers, arrange them such that they form the largest num ...

  5. [LeetCode] Largest Number 排序

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  6. [LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数

    In a given integer array nums, there is always exactly one largest element. Find whether the largest ...

  7. LeetCode() Largest Number

    全排列,超时,知道超时,只是想验证一下. class Solution { public: string largestNumber(vector<int>& nums) { so ...

  8. leetcode 179. Largest Number 求最大组合数 ---------- java

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  9. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

随机推荐

  1. js实现StringBuffer

    实现 function StringBuffer() { this.__strings__ = []; }; StringBuffer.prototype.Append = function (str ...

  2. Eclipse 实用技巧

    代码智能提示 Java智能提示 Window -> Preferences -> Java -> Editor -> Content Assist -> Auto Act ...

  3. STM32F746的RTC使用

    1.RTC模块采用低速晶振外接始终:32.768KHz,如下图所示 2.配置RTC模块: 其中,Fck_apre.Fck_spre始终上配置不容易理解, 如果想得到1Hz的始终频率,则需要将PERDI ...

  4. [jQuery]jQuery DataTables插件自定义Ajax分页实现

    前言 昨天在博客园的博问上帮一位园友解决了一个问题,我觉得有必要记录一下,万一有人也遇上了呢. 问题描述 园友是做前端的,产品经理要求他使用jQuery DataTables插件显示一个列表,要实现分 ...

  5. BZOJ3095 : 二元组

    \[\begin{eqnarray*}&&\sum_{i=0}^{n-1}\left(ki+b-a_i\right)^2\\&=&\sum_{i=0}^{n-1}\le ...

  6. Debian8安装Vim8

    1 安装vim需要的库 apt-get build-dep vim-gtk apt-get install libncurses5-dev mercurial   2 下载Vim8 apt-get i ...

  7. mysql常处理用时间sql语句

    Mysql日期函数,时间函数使用的总结,以及时间加减运算(转) select timediff('23:40:00', ' 18:30:00'); -- 两时间相减SELECT substring( ...

  8. CartO

    Carto documentation The following is a list of properties provided in CartoCSS that you can apply to ...

  9. 利用github pages创建简单的网站

    github.com 作为最流行的源代码管理工具已经风靡全球,同时在依托于github也衍生出了各种各样的应用,比如可以利用github搭建博客系统等等. 先换个话题,我们每人手头都或多或少有些&qu ...

  10. 安卓DJ113舞曲网应用客户端 项目源码(服务器+客户端)

    Android DJ113舞曲网app客户端 播放器源码  项目源码(服务器+客户端),这个项目整体有点类似天天动听的效果,非常漂亮的,支持第三方登录等功能,非常完整的一个音乐项目. 源码下载:htt ...