*Largest Number
2015-10-09 04:12
232 查看
Given a list of non negative integers, arrange them such that they form the largest number.
For example, given
Note: The result may be very large, so you need to return a string instead of an integer.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
class NumbersComparator implements Comparator<String> { @Override public int compare(String s1, String s2) { return (s2 + s1).compareTo(s1 + s2); } } public class Solution { public String largestNumber(int[] nums) { String[] strs = new String[nums.length]; for (int i = 0; i < nums.length; i++) { strs[i] = Integer.toString(nums[i]); } Arrays.sort(strs, new NumbersComparator()); StringBuilder sb = new StringBuilder(); for (int i = 0; i < strs.length; i++) { sb.append(strs[i]); } String result = sb.toString(); int index = 0; while (index < result.length() && result.charAt(index) == '0') { index++; } if (index == result.length()) { return "0"; } return result.substring(index,result.length()); } }
相关文章推荐
- 发布网站之后,局域网内其他机器无法访问的解决办法
- 电商移动营销:覆盖和穿透用户是对品牌新的挑战
- 浅谈计算机中的存储模型(二)物理内存
- jQuery源码 support
- :: [QQConnect] > access_token丢失 : _getMeError 怎么办?
- ios 修改navigationBar返回键 ' < ' 的颜色
- gcc编译选项的循环重复查找依赖库等命令
- 1.3java语言基础(下)
- 1.2java语言基础(上)
- SQL Server 服务无法启动。有关详细信息,请参阅 SQL Server 联机丛书中的主题“如何查看 SQL Server 2005 安装日志文件”和“手动启动 SQL Server”
- [Leetcode] Peeking Iterator
- ZOJ 3642 Just Another Information Sharing Problem (最大流)
- ZOJ 3641 Information Sharing (并查集 + set)
- 数组实现循环队列(Java)
- ZOJ 3640 Help Me Escape (期望dp)
- LeetCode-Merge Intervals
- ubuntu 14.04 部署ssh samba
- ZOJ 3637 Education Manage System (dp+线段树)
- Implement Stack using Queues 解答
- ZOJ 3633 Alice's present (脑洞题)