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. oracle数据库表空间的创建与使用

     以下操作请使用sys系统账号操作! 1. 查询物理存储的位置 SELECT tablespace_name, file_id, file_name, round(bytes / (1024 * 10 ...

  2. LINQ操作符二:SelectMany

    SelectMany操作符提供了将多个from子句组合起来的功能,相当于数据库中的多表连接查询,它将每个对象的结果合并成单个序列. 示例: student类: using System; using ...

  3. html2canvas如何在元素隐藏的情况下生成截图

    html2canvas官网地址:http://html2canvas.hertzen.com/ github地址:https://github.com/niklasvh/html2canvas/ 从官 ...

  4. selenium测试(Java)-- 隐式等待(十)

    隐式等待相当于设置全局的等待,在定位元素时,对所有元素设置超时时间. 隐式等待使得WebDriver在查找一个Element或者Element数组时,每隔一段特定的时间就会轮询一次DOM,如果Elem ...

  5. tensorflow函数解析:Session.run和Tensor.eval的区别

    tensorflow函数解析:Session.run和Tensor.eval 翻译 2017年04月20日 15:05:50 标签: tensorflow / 机器学习 / 深度学习 / python ...

  6. TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习

    转载http://blog.csdn.net/jerr__y/article/details/60877873 1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf ...

  7. github 远程仓库操作

    工作中需要在github上保存项目,一个仓库中有多个分支,进行一些实验,方便后面操作. 参考链接 http://rogerdudler.github.io/git-guide/index.zh.htm ...

  8. git call failed: [git clone Could not resolve host: git.openstack.org

    git config --global http.proxy $http_proxy git config --global https.proxy $https_proxy ref: http:// ...

  9. ubuntu安裝 R RStudio

    sudo apt--i386.deb ref: http://blog.csdn.net/lichangzai/article/details/39376117

  10. Project Navigator Help: Creating a Workspace in Xcode

    Creating a Workspace Start a multiproduct development endeavor by creating a workspace. 1.Choose Fil ...