Leetcode Largest Number
2016-07-19 04:43
295 查看
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.
Difficulty: Medium
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.
Difficulty: Medium
public class Solution { public String largestNumber(int[] nums) { String[] list = new String[nums.length]; for(int i = 0; i < nums.length; i++){ list[i] = Integer.toString(nums[i]); } Arrays.sort(list, new Comparator<String>(){ public int compare(String s1, String s2){ String leftRight = s1+s2; String rightLeft = s2+s1; return -leftRight.compareTo(rightLeft); } }); String ans = ""; for(String str : list){ ans = ans + str; } if(ans.length() == 0 || ans.charAt(0) == '0') return "0"; return ans; } }
相关文章推荐
- Leetcode Word Break II
- [leetcode] 118. Pascal's Triangle
- php判断是否为今天
- Codeforces Round #327 (Div. 2)
- 决策树算法(四)——选取最佳特征划分数据集
- 一键安装LAMP或LNMP
- [leetcode] 110. Balanced Binary Tree
- Leetcode 299. Bulls and Cows (Python)
- redhat7.2用户及用户组权限管理操作实例
- zabbix redis_low_level_discovery
- OSChina 周二乱弹 ——风流不需编码强,红袖无言意最长
- rename datafile 做了什么
- Leetcode 373. Find K Pairs with Smallest Sums(Python)
- [LeetCode] 66. Plus One
- CnCrypt代码 之 为磁盘设备分配和移除盘符
- HDU 2203 亲和串
- HDU 2087 KMP求匹配串的重复次数
- HDU 1711 KMP求匹配位置
- 发朋友圈的九条建议
- HDU 1686 求子串的数量