[每日编程]求 largest Number - 给出一组非负整数,求这些非负整数可以拼接出的最大数字
2016-05-07 22:24
501 查看
英文:Given a list of non negative integers, arrange them such that they form the largest number.
中文:给出一组非负整数,求这些非负整数可以拼接出的最大数字
说明:例如,给出数组 [3, 30, 34, 5, 9],拼接出的最大数字为9534330
正确的排序方法,是使用排序方法进行比较时,比较两个字符串(设为A和B),以先后顺序拼接而成的两个字符串A+B和B+A,如果A+B更大,则A在前B在后,否则A在后B在前。 public class Solution { public static String largestNumber(int[] nums) { String[] array = new String[nums.length]; for(int i = 0; i < nums.length; i++) { array[i] = String.valueOf(nums[i]); } String temp; for(int i = 0; i < array.length; i++) { for(int j = i+1; j < array.length; j++) { if((array[i] + array[j]).compareTo(array[j] + array[i]) < 0) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } if(array[0].equals("0")) return "0"; else { return String.join("", array); } } public static void main(String[] args) { int[] nums = {3, 30, 34, 5, 9}; String largestNum = largestNumber(nums); System.out.println(largestNum); } }
相关文章推荐
- 2016/5/7 PHP入门了解
- django 视图开发与url配置
- Java的代理—JDK Proxy
- 使用scanf()函数应注意的问题??
- java 完全解耦和弱耦合
- python基本数据类型之集合set
- count_if 和count
- C++上机实验5-项目五
- Python3安装xlutils
- HDU 1056 - HangOver
- 腾讯C++代码规范
- Java Swing 秒表
- python入门学习课程推荐
- 用C++语言写游戏——打飞机
- 维护代码简洁,使用lombok消除冗长的Java代码
- Java学习4_一些基础4_输入输出_16.5.7
- 如何定义 Java 中的方法(二)
- 关于在Ubuntu下安装配置numpy,scipy,matplotlibm,pandas 以及sklearn
- django
- java Io流中对象序列化和反序列问题