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. 常用shell命令实战

    #!/bin/sh ################### #### 环境变量 ### ################### #主程序目录 #APP_HOME=/apps/svr/apache-to ...

  2. RunnableException与CheckedException

    Checked Exception 编译时异常 编译的时候检查你的代码可能在运行的时候抛出异常,这通常在编译的时候要去处理的. RunnableException 运行时异常,可以编译通过,但如果不处 ...

  3. selenium测试(Java)--学习总结

  4. Geometric deep learning on graphs and manifolds using mixture model CNNs

    Monti, Federico, et al. "Geometric deep learning on graphs and manifolds using mixture model CN ...

  5. C++编程 - tuple、any容器

    C++编程 - tuple.any容器 flyfish 2014-10-29 一 tuple tuple是固定大小的容器,每一个元素类型能够不同 作用1 替换struct struct t1 { in ...

  6. PHP curl_setopt函数用法介绍上篇

    最近,学习与实践了php中curl的知识点.在此做个初步的总结: 先看看对于它的基本介绍: curl_setopt函数是php中一个重要的函数,它可以模仿用户的一些行为,如模仿用户登录,注册等等一些用 ...

  7. php中常用$_SERVER的用法

    #测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br> ...

  8. hbase学习 rowKey的设计-4

    访问hbase table中的行,只有三种方式: 1 通过单个row key访问 2 通过row key的range 3 全表扫描 Hadoop Sequence File 文中可能涉及到的API: ...

  9. Python学习笔记(一)——基本知识点

    主要记录学习Python的历程和用于复习.查阅之用. 知识点: 数据类型(列表.元组.字典.集合) 帮助文档 函数(默认参数.可变参数.关键字参数.参数组合) 数据类型: 列表:list       ...

  10. 如何快捷地使用ChemBio 3D检查结构信息

    ChemBio 3D是一款三维分子结构演示软件,能够轻松快捷地进行化学结构的制作和立体旋转.ChemBio 3D Ultra 14作为ChemBio 3D的最新版本可以更加快捷地制作化学结构.本教程将 ...