Description:

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.

把所给的数字排列成最大的数,首先想到的思路是按位比较,但是那样比较麻烦而且容易出BUG,所以还有一种简单的思路就是在排序的时候比较两种情况前后排列取结果最大的那种顺序。代码少好理解。

public class Solution {
public String largestNumber(int[] nums) {
ArrayList<Number> list = new ArrayList<Number>();
for(int i : nums) {
list.add(new Number(i+""));
}
Collections.sort(list);
StringBuilder sb = new StringBuilder();
for(int i=list.size()-1; i>=0; i--) {
sb.append(list.get(i).val);
}
java.math.BigInteger b = new java.math.BigInteger(sb.toString());
return b.toString();
}
}
class Number implements Comparable<Number> {
String val;
public Number(String val) {
this.val = val;
}
public int compareTo(Number n) {
java.math.BigInteger a = new java.math.BigInteger(this.val + n.val);
java.math.BigInteger b = new java.math.BigInteger(n.val + this.val);
return a.compareTo(b);
}
}

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

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

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

  2. Leetcode Largest Number c++ solution

    Total Accepted: 16020 Total Submissions: 103330     Given a list of non negative integers, arrange t ...

  3. LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用

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

  4. [LeetCode] Largest Number 排序

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

  5. [LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数

    In a given integer array nums, there is always exactly one largest element. Find whether the largest ...

  6. LeetCode() Largest Number

    全排列,超时,知道超时,只是想验证一下. class Solution { public: string largestNumber(vector<int>& nums) { so ...

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

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

  8. Leetcode:Largest Number详细题解

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

  9. [LeetCode][Python]Largest Number

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

随机推荐

  1. /dev/null脚本中作用

    /dev/null设备文件只有一个作用,往它里面写任何数据都直接丢弃. 因此保证了该命令执行时屏幕上没有任何输出. 在shell中常见. command  > /dev/null   2> ...

  2. eclipse安装中文补丁包

    第一步:打开http://www.eclipse.org/babel/ 第二步:找到downloads. 第三步:点击Oxygen. 第四步:找到简体中文的zip插件并兵下载. 第五步:解压. 第六步 ...

  3. shell命令之根据字符串查询文件对应行记录

    显示xxx字符串对应的行数,并向前打印3行,向后打印2行,查找对应文件为filename.txt 命令:grep -n 'xxx' -A3 -B2 --color=auto filename.txt ...

  4. OpenResty(Nginx+Lua)开发入门

    Nginx入门 本文目的是学习Nginx+Lua开发,对于Nginx基本知识可以参考如下文章: nginx启动.关闭.重启 http://www.cnblogs.com/derekchen/archi ...

  5. 检查 Linux 服务器性能

    如何用十条命令在一分钟内检查 Linux 服务器性能 如果你的Linux服务器突然负载暴增,报警短信快发爆你的手机,如何在最短时间内找出Linux性能问题所在?来看Netflix性能工程团队的这篇博文 ...

  6. pip国内源

    pip install -i https://pypi.douban.com/simple pyqrcode

  7. ansible register when: result | succeeded when: item.rc != 0

    ansible register 这个功能非常有用.当我们需要判断对执行了某个操作或者某个命令后,如何做相应的响应处理(执行其他 ansible 语句),则一般会用到register . 举个例子: ...

  8. Hibernate- hibernate二级缓存

    原文地址:http://www.iteye.com/topic/18904 很多人对二级缓存都不太了解,或者是有错误的认识,我一直想写一篇文章介绍一下hibernate的二级缓存的,今天终于忍不住了. ...

  9. 用python批量执行VBA代码

    先说下背景环境 1. 公司需要问卷调查,有两份问卷, 1)是spss问卷,2)是excel问卷.spss问卷数据不全,但有各种标签, excel呢, 生成的数据直接把选项变成了值 2. 现在需要把ex ...

  10. h264 i p 帧特点

     1.爱无铭(47530789) 2014-2-13 17:07:27 I帧只有intra p帧有inter和intra:B帧一般只用inter  2.  庐舍闲士(361389535) 2014-2 ...