【LeetCode】Largest Number 解题报告

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/largest-number/#/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.

Ways

题目意思是对数字进行重排拼接组成最大数字。
首先我想到的就是对每个数字的第一位进行排序,然后如果有第二位再对第二位排序等等。如果纯int操作的话,因为数字的位数不同,这样做会很麻烦。所以我想到了转换成String再比较。
如果是直接比较字符串a,b的大小,也是不可以的。比如a="30",b="3",直接字符串比较结果为a>b组成"303",而目标的是b>a组成"330"。因此最终想到比较的是a+bb+a
对于上面的列子,a+b="303",b+a="330",可以看出b+a>a+b(a+b).compareTo(b+a)返回的是负,而对于sort(),返回负是从小到大排序,不合题意。故应该是(b+a).compareTo(a+b),这样是从大到小排序。
排序之后把所有结果拼接组成一个字符串即可。最后要注意的问题是可能出现[0,0]这样的输入,结果是"00",因此,如果最终结果的首位是'0',那么应该修改输出的结果为"0".

public class Solution {
public String largestNumber(int[] nums) {
String[] s = new String[nums.length];
for (int i = 0; i < nums.length; i++) {
s[i] = Integer.toString(nums[i]);
}
Arrays.sort(s, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return (b + a).compareTo(a + b);
}
});
StringBuilder ans = new StringBuilder();
for (String temp : s) {
ans.append(temp);
}
if (ans.charAt(0) == '0') {
ans = new StringBuilder("0");
}
return ans.toString();
}
}

Date

2017 年 4 月 5 日

【LeetCode】Largest Number 解题报告的更多相关文章

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

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

  2. 【LeetCode #179】Largest Number 解题报告

    原题链接:Largest Number 题目描述: Given a list of non negative integers, arrange them such that they form th ...

  3. 【原创】leetCodeOj --- Largest Number 解题报告

    原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...

  4. LeetCode: Valid Number 解题报告

    Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  5. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  6. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  7. 【九度OJ】题目1040:Prime Number 解题报告

    [九度OJ]题目1040:Prime Number 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1040 题目描述: Ou ...

  8. [LeetCode] 179. Largest Number 解题思路

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

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

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

随机推荐

  1. shell 脚本在linux中的应用

    shell脚本在linux中应用广泛,之前一直选用python写脚本来进行一些文件操作,但是最后发现shell脚本非常方便,所以特意来学习下皮毛,便于提高自己效率 定义变量 1 country=&qu ...

  2. SourceTree git 工作流

    转载自:https://www.cnblogs.com/tian-xie/p/6264104.html 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 win ...

  3. 45-Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution Total Accepted: 7855 ...

  4. Yarn 容量调度器多队列提交案例

    目录 Yarn 容量调度器多队列提交案例 需求 配置多队列的容量调度器 1 修改如下配置 SecureCRT的上传和下载 2 上传到集群并分发 3 重启Yarn或yarn rmadmin -refre ...

  5. 用前端表格技术构建医疗SaaS 解决方案

    电子健康档案(Electronic Health Records, EHR)是将患者在所有医疗机构产生的数据(病历.心电图.医疗影像等)以电子化的方式存储,通过在不同的医疗机构之间共享,让患者面对不同 ...

  6. A Child's History of England.28

    By such means, and by taxing and oppressing the English people in every possible way, the Red King b ...

  7. E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing

    解决办法:apt-get update或者apt-get cleanapt-get update 或者 apt-get update --fix-missing问题解析1 source本身的问题 根据 ...

  8. mysql_取分组后的前几行值

    --方法一: select a.id,a.SName,a.ClsNo,a.Score from Table1 a left join Table1 b on a.ClsNo=b.ClsNo and a ...

  9. new Date()与setDate()参数

    New Date()与setDate()参数 相信网上已经有很多关于日期的文章了,这里只是我自己再工作中遇到的问题然后加以总结: new Date() new Date() 一共有六种形式,五种带参数 ...

  10. JS - 事件常用

    问:什么是事件? 答:JS创建动态页面,可以被JS侦测到的行为.网页中的每个元素都可以产生某些可以触发JS函数的事件.比如说,当用户点击按钮时,就发生一个鼠标单击(onclick)事件,需要浏览器做出 ...