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.

题目大意:给定一个数组,数组里都是非负整数,求一个组合使得这个数是最大的。

解题思路:这个题一开始我是自定义class然后写comparator,首先比较第一位数,然后比较第二位……后来想了一下其实直接把两个数转为String,然后分别前后拼接一下就可以比较了。直接sort就可以得到结果,代码更清晰。或者定义一个PriorityQueue,自定义comparator,把给定的数组转为这个queue也是一样的。

    public String largestNumber(int[] nums) {
if (nums == null || nums.length < 1) {
return null;
}
LinkedList<String> res = new LinkedList<>();
for (int num : nums) {
res.add(String.valueOf(num));
}
StringBuilder sb = new StringBuilder();
Collections.sort(res, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String s1 = o2 + o1;
String s2 = o1 + o2;
return s1.compareTo(s2);
}
});
for (String next : res) {
sb.append(next);
}
int cnt = 0;
while (cnt < sb.length() && sb.charAt(cnt) == '0') {
cnt++;
}
return sb.substring(cnt == sb.length() ? cnt - 1 : cnt);
}

Largest Number——LeetCode的更多相关文章

  1. Largest Number || LeetCode

    #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 1000 int cm ...

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

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

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

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

  4. Leetcode:Largest Number详细题解

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

  5. [LeetCode][Python]Largest Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...

  6. LeetCode之“排序”:Largest Number

    题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...

  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 、剑指offer33 把数组排成最小的数

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

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

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

随机推荐

  1. TCP/IP源码(59)——TCP中的三个接收队列

    http://blog.chinaunix.net/uid-23629988-id-3482647.html TCP/IP源码(59)——TCP中的三个接收队列  作者:gfree.wind@gmai ...

  2. 使用downloadmanager调用系统的下载

    /** * 文件名 UpdateDownload.java * 包含类名列表 com.issmobile.numlibrary.tool * 版本信息  版本号  * 创建日期 2014年7月14日  ...

  3. 如何解决eclipse上的Android程序“Please ensure that adb is correctly located at 'D:\eclipse\sdk\platform-tools\adb.exe' and can be executed.”小问题?

    首先,把运行的Android模拟器和eclipse一块儿关了, 然后win+R,cmd, 下面输入adb kill_server 再输入adb start_server 之后重新运行项目,不出意外的话 ...

  4. 关于uploadify 没有显示进度条!!!!

    如果你也弄了很久不知道为什么不出现上传进度条!,那就一定要看这里了! 我注释了 queueID 属性后 就出现了!!!!! 就是这么简答! //添加界面的附件管理 $('#file_upload'). ...

  5. Excel.Application手册

    ----转载:http://blog.csdn.net/xxfigo/article/details/6618129 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明 ...

  6. centos5.2 x86 安装 oracle 11g2r 日志

    一.安装centos 二.安装ora所需的库 三.修改centos内核 四.建用户组和目录结构等 五.安装ora11g2r 六.安装sqlplus的翻页程序和help补丁 七.自启动脚本 八.常用命令 ...

  7. oracle查询表信息(索引,外键,列等)

    oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息,索引信息查询SQL如下,希望对大家有所帮助:1.查询出所有的用户表 select * from user_tables ...

  8. addEventListener之handleEvent

    addEventListener() 方法是将指定的事件监听器注册到目标对象上,当该对象触发指定的事件时,指定的回调函数就会被执行.语法: element.addEventListener(type, ...

  9. 二十分钟弄懂C++11 的 rvalue reference (C++ 性能剖析 (5))

    C++ 11加了许多新的功能.其中对C++性能和我们设计class的constructor或assignment可能产生重大影响的非rvalue reference莫属!我看了不少资料,能说清它的不多 ...

  10. photpshop渐变玩法_学习教程