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. tsql 循环id读取

    declare @IDList as varchar(max) declare @ID as int declare @i as int set @IDList='' )) + ',' from ta ...

  2. IT职业发展攻略(技术仅是工具而已)

    时光飞逝,我事业中第一个十年就快结束了.在这十年里,让我收获了很多,今天想与大家分享一下,我在 IT 职场方面的一些个人经验,不一定对每个人都实用,请大家仅作参考吧. 大家既然都是做技术的,那我们不妨 ...

  3. 第6章 进程控制(3)_wait、exec和system函数

    5. 等待函数 (1)wait和waitpid 头文件 #include <sys/types.h> #include <sys/wait.h> 函数 pid_t wait(i ...

  4. Doris FE负载均衡配置

    0 背景概述 Doris完全兼容了mysql协议,并且Doris FE本身通过多follower选举机制选举出master,可以保证fe本身的高可用性,也可以通过加入observer fe节点来提高f ...

  5. CSS3 盒阴影(box-shadow)详解

    CSS3 的 box-shadow 有点类似于 text-shadow,只不过不同的是 text-shadow 是对象的文本设置阴影,而 box-shadow 是给对象实现图层阴影效果.本文我们搁下I ...

  6. php printf() 输出格式化的字符串

    php printf() 函数用于输出格式化的字符串,本文章向码农介绍php printf()函数的使用方法和基本使用实例,感兴趣的码农可以参考一下. 定义和用法 printf()函数输出格式化的字符 ...

  7. Python - Django - 使用 Pycharm 连接 MySQL 数据库

    在 Pycharm 的右上方找到 Database 点击 依次点击,选择 MySQL 数据库 点击 Download 下载驱动文件 下载完成后对数据库的相关信息进行填写 填写完成后点击“Test Co ...

  8. 初试mysql5.7.2新特性:多源复制(MySQL 5.7 multi-source replication)

    多源复制和多主复制的区别: 多主复制示意图: 多源复制示意图: 在my.cnf中添加crash safe特性参数:master_info_repository=TABLE;relay_log_info ...

  9. oracle11gr2笔记(一)

    一,使用scoot用户被锁.解决办法:(http://ciiiso.blog.51cto.com/8779682/1432869/) 二,使用root用户登录系统无法sqlplus,提示说permis ...

  10. Window python下载安装

    Window python下载安装 http://www.runoob.com/python/python-install.html https://pan.baidu.com/s/1MoR9nWUY ...