把数组排成最小的数
来源:互联网 发布:sql找不到存储过程 编辑:程序博客网 时间:2024/06/18 15:47
题目描述:
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
参考代码:
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;public class Test { public static String PrintMinNumber(int[] numbers) { ArrayList<String> list = new ArrayList<>(); for(int i = 0; i < numbers.length; i++){ list.add(String.valueOf(numbers[i])); } Collections.sort(list, new Comparator<String>() { public int compare(String o1, String o2) { Integer c1 = new Integer(o1 + "" + o2); Integer c2 = new Integer(o2 + "" + o1); if(c1 > c2){ return 1; }else{ return -1; } } }); String result = ""; for(int i = 0; i < list.size(); i++){ result += list.get(i); } return result; } public static void main(String[] args) { int[] a = {3,321,32}; System.out.println(PrintMinNumber(a)); }}
阅读全文
1 0
- 把数组排成最小的数--总结
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数--总结
- 把数组排成最小的数--总结
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 1.2、class文件中的常量池
- 提交方式的注入
- ViewGroup事件分发
- 实验吧try them all
- D
- 把数组排成最小的数
- C语言基础之结构体
- 解决pyenv安装pyhton下载安装缓慢的问题
- Activemq 访问后台出现503错误
- java web 上传文件的中文名乱码纪要
- 1.2.2、Class文件中的常量池详解(上)
- cvte-c++-笔试1
- Nginx之rpm安装-yellowcong
- Uva 10815(string,set,vector,unique)安迪的第一个字典