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. linux如何查看系统是多少位的?64 OR 32

    1.可以用命令“getconf LONG_BIT”查看, 如果返回的结果是32则说明是32位,返回的结果是64则说明是64位. 2.此外还可以使用命令“uname -a”查看, 输出的结果中,如果有x ...

  2. Microsoft Enterprise Library

    http://entlib.codeplex.com/ 微软企业库 现在已经到到6版本了 2013年更新的. https://www.microsoft.com/en-us/download/conf ...

  3. jQuery 的noConflict()的使用.

    我们项目现在需要用到两个js库.一个是jQuery库,还有一个是我们自己开发的轻量级的gys.js库. 而gys库对外提供的接口也是$符号.和jQuery库是一样的,这个时候,两个库就会发生冲突了,我 ...

  4. 自定义ExtJS插件

    http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f73b6f93834c28c3933fc239045647 ...

  5. 在windows 下将 chm 格式的文件 转换成 html 的文件

    有时我们可能需要将 chm 格式的文件 转换成 html 格式的网页文件,这时,如果你使用的是 windows 操作系统,那就可以用 windows 操作系统自带的反编译工具来完成这项任务,具体步骤: ...

  6. 3.1_分类算法之k-近邻

    分类算法之k-近邻 k-近邻算法采用测量不同特征值之间的距离来进行分类 优点:精度高.对异常值不敏感.无数据输入假定 缺点:计算复杂度高.空间复杂度高 使用数据范围:数值型和标称型 一个例子弄懂k-近 ...

  7. 使用Travis进行持续集成

    使用Travis进行持续集成 廖雪峰 持续集成:Continuous Integration,简称CI,意思是,在一个项目中,任何人对代码库的任何改动,都会触发CI服务器自动对项目进行构建,自动运行测 ...

  8. chrome 设置是否缓存

    在进行本地开发时,因老需要修改js,css等文件,而页面又带有缓存因此无法自动更新为新的文件. 在页面点击 -> F12 ->F1 ->References -> NetWor ...

  9. python, Django csrf token的问题

    环境 Window 7 Python2.7 Django1.4.1 sqlite3 问题 在使用Django搭建好测试环境后,写了一个提交POST表单提交留言的测试页面. 如图: 填写表单,点击“提交 ...

  10. hadoop配置文件的参数含义说明

    #hadoop version 查看版本号 1 .获取默认配置 hadoop2系列配置文件一共包括6个,分别是hadoop-env.sh.core-site.xml.hdfs-site.xml.map ...