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. myeclipse html乱码

    myeclispe 中 html乱码 在页面的开头写上即可 <meta http-equiv="content-type" content="text/html;  ...

  2. Android(java)学习笔记226:服务(service)之为什么使用服务

    1.服务 service 长期在后台运行的进程,一般没有应用程序界面   2.进程线程和应用程序之间的关系 应用程序开启,系统启动一个Linux进程,所有的组件都是运行在同一个进程的同一个线程(mai ...

  3. ie兼容CSS3渐变写法

    在css3之前要想做背景色渐变就只能采用添加背景图片的方法,但是随着css3:linear-gradient属性的出现,就可以避免使用添加背景图片的方法,从而优化了性能.但是inear-gradien ...

  4. 【转】自动布局之autoresizingMask使用详解(Storyboard&Code)

    原文:http://www.cocoachina.com/ios/20141216/10652.html 自动布局Autolayoutstoryboard 前言:现在已经不像以前那样只有一个尺寸,现在 ...

  5. PHP MySQL 读取数据

    PHP MySQL 读取数据 从 MySQL 数据库读取数据 SELECT 语句用于从数据表中读取数据: SELECT column_name(s) FROM table_name 如需学习更多关于 ...

  6. makecert 制作数字证书

    在MS的SDK6.0中有个证书生成工具makecert.exe, 你可以使用这个工具来生成测试用的证书. 第一步,生成一个自签名的根证书(issuer,签发者). >makecert -n &q ...

  7. 解决 asp.net 伪静态 IIS设置后 直正HTML无法显示的问题

    asp.net+UrlRewriter来实现网站伪静态,实现伪静态有一些好处,比如对于搜索引擎更好收录页面,还有一个好处就是隐藏了真实地址的参数,所以有独立服务器的朋友,配置IIS实现伪静态功能,挺不 ...

  8. Wireshark抓包、过滤器

    查阅于http://blog.sina.com.cn/s/blog_5d527ff00100dwph.html 1.捕捉过滤器 设置捕捉过滤器的步骤是:- 选择 capture -> optio ...

  9. 对于HttpContext.Current的一点理解

    string[] userInfomationSplits = HttpContext.Current.User.Identity.Name.Split(new string[] { "\\ ...

  10. Firefox中Vimperator插件配置

    具体配置什么,同学们可以网上看下善用佳软关于Vimperator的说明,在这里我列出两条我个人觉得最有用的命令 set nextpattern=\s*下一页|下一张|下一篇|下页|后页\s*,^\bn ...