【LeetCode】Largest Number 解题报告
【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+b
和b+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 解题报告的更多相关文章
- LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- 【LeetCode #179】Largest Number 解题报告
原题链接:Largest Number 题目描述: Given a list of non negative integers, arrange them such that they form th ...
- 【原创】leetCodeOj --- Largest Number 解题报告
原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...
- LeetCode: Valid Number 解题报告
Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【九度OJ】题目1040:Prime Number 解题报告
[九度OJ]题目1040:Prime Number 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1040 题目描述: Ou ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [LeetCode] Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
随机推荐
- shell 脚本自动插入文件头
vim编辑shell脚本自动插入文件头部信息,将下面的代码写入home目录xia .vimrc 文件即可. shell 文件头: 1 autocmd BufNewFile *.sh exec &quo ...
- NFS FTP SAMBA的区别
Samba服务 samba是一个网络服务器,用于Linux和Windows之间共享文件. samba端口号 samba (启动时会预设多个端口) 数据传输的TCP端口 139.445 进行NetBIO ...
- 【翻译】.NET 6 中的 dotnet monitor
原文:Announcing dotnet monitor in .NET 6 我们在 2020 年 6 月首次推出了dotnet monitor 作为实验工具,并在去年(2020年)努力将其转变为生产 ...
- idea数据库报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
通过idea操作数据库,进行数据的增加,运行时报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 原因:没有导入mysql-connec ...
- words in English that contradict themselves
[S1E10, TBBT]Leonard: I don't get it. I already told her a lie. Why would I replace it with a differ ...
- adjust, administer
adjust to just, exact. In measurement technology and metrology [度量衡学], calibration [校准] is the compa ...
- day13 cookie与session和中间件
day13 cookie与session和中间件 今日内容概要 cookie与session简介 django操作cookie与session django中间件简介 如何自定义中间件 csrf跨站请 ...
- Hbase(二)【shell操作】
目录 一.基础操作 1.进入shell命令行 2.帮助查看命令 二.命名空间操作 1.创建namespace 2.查看namespace 3.删除命名空间 三.表操作 1.查看所有表 2.创建表 3. ...
- 双向循环链表模板类(C++)
双向链表又称为双链表,使用双向链表的目的是为了解决在链表中访问直接前驱和后继的问题.其设置前驱后继指针的目的,就是为了节省其时间开销,也就是用空间换时间. 在双向链表的每个节点中应有两个链接指针作为它 ...
- Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...