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

Input: [10,2]
Output: "210"
Input: [3,30,34,5,9]
Output: "9534330"

题意:

给定一堆数字,将它们排列顺序,使其连一块儿能形成最大的数。

思路:

先转化成一个字符串数组

然后把这个数组按特定规则排个序(熟练使用字符串的compareTo方法)

最后建一个StringBuilder

将排好序的字符串按顺序加到StringBuilder后面(注意字符串开头可能为0的极端情况)

最后将StringBuilder转成String输出

代码:

 class Solution {
public String largestNumber(int[] a) { // int[] --> String[]
String[] array = new String[a.length];
for(int i = 0; i< a.length; i++){
array[i] = a[i] +"";
} // sort string
Arrays.sort(array, (sA, sB)->(sB + sA).compareTo(sA + sB)); // string[] -> result
StringBuilder sb = new StringBuilder();
for (String s : array) {
sb.append(s);
} return sb.charAt(0) == '0' ? "0" : sb.toString();
}
}

[leetcode]179. Largest Number最大数的更多相关文章

  1. leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数

    这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...

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

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

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

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

  4. Java 特定规则排序-LeetCode 179 Largest Number

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

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

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

  6. Java for LeetCode 179 Largest Number

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

  7. [LeetCode] 179. Largest Number 解题思路

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

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

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

  9. Leetcode 179 Largest Number 贪心

    此题主要是讲给你一组数,如何将数连在一起能得到最大值(最小值反之),注意局部最优,就是说如果 123 234两个连在一起怎么样最大,显然是234123比123234大,对于3个数我们可以找到类似的性质 ...

随机推荐

  1. nginx的日志分析

    1.到NGINX把日志DOWN下来2.用命令cat xxxx.log | egrep '10/Jul/2015:01:[4-5]|2015-07-10 02:0[0-57]'>xxxx2.log ...

  2. JpGraph使用详解http://5ydycm.blog.51cto.com/115934/177498 http://www.cnblogs.com/txw1958/archive/2013/08/18/php-charts.html

    下载 在官方网站 http://www.aditus.nu/jpgraph/ 下载jpgraph,其中1.X系列是用于PHP4的,2.X系列是用于PHP5的. 安装 将下载的得到的jpgraph压缩文 ...

  3. Lua语言中的__index,__newindex,rawget和rawset

    转自:http://blog.csdn.net/wangbin_jxust/article/details/12108189 在谈及Lua中的__index,__newindex,rawget和raw ...

  4. Lua C++互传结构体实例

    转自:http://bbs.csdn.net/topics/350261649 =====main.cpp======= #include "stdio.h" extern &qu ...

  5. decorator的class方式

    class式的 Decorator decorator的class方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 clas ...

  6. C# 中 PadLeft和PadRight 的用法

    C# 中 PadLeft和PadRight 的用法 在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char pa ...

  7. 链接mysql的两种方法

    使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库. 实例 以下是从命令行中连接mysql服务器的简单实例: [root@host]# my ...

  8. 信息学奥赛(NOIP)初赛学习方法推荐

    首先声明:本帖针对初学者,本帖只是列出一个大概的框架,不属于自学方法,有条件有能力,请找一位好老师来教,多跟前辈交流经验.(否则多会出现事倍功半的悲剧!) 一.初赛内容 初赛偏重于基础知识. 1. 一 ...

  9. jmeter测试20个QPS下的响应时间-设置QPS限制

    添加--->定时器--->Constant Throughput Timer Constant Throughput Timer 的主要属性介绍: 名称:定时器的名称 Target thr ...

  10. bootstrap3中select2的默认值和下拉框的禁用

    最近做项目用到了select2插件,需求中需要给下拉框设置默认值之后,禁用下拉框,我开始的写法是这样的 <script type="text/javascript"> ...