把数组排成最小的数
来源:互联网 发布:php中截取字符串 编辑:程序博客网 时间:2024/06/07 09:46
题目描述:
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
解题思路:
将数组元素存入动态数组,然后调用集合的sort()的方法,实现Comparator接口的compare方法,将集合元素按照compare规则进行排序。然后将元素进行拼接。
public class Solution33 { public static String PrintMinNumber(int [] numbers) { String s = ""; ArrayList<Integer> arrayList = new ArrayList<Integer>(); for (int i=0;i<numbers.length;i++){ arrayList.add(numbers[i]); } //实现了Comparator接口的compare方法,将集合元素按照compare方法的规则进行排序 Collections.sort(arrayList,new Comparator<Integer>(){ @Override public int compare(Integer str1,Integer str2){ String s1 = str1+""+str2; System.out.println(s1); String s2 = str2+""+str1; //System.out.println(s2); return s1.compareTo(s2); //s1大于s2,返回1 } }); System.out.println("排序之后集合的元素:"+arrayList); //排序之后集合的元素:[321, 32, 3] for (int j : arrayList){ //将排序之后的元素拼接在一起 s+=j; System.out.println(s); } return s; } public static void main(String args[]){ int[] array = {3,32,321}; PrintMinNumber(array); } }
0 0
- 把数组排成最小的数--总结
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数--总结
- 把数组排成最小的数--总结
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- poj 2486 树形dp
- Android媒体库使用分组查询
- 对抗网络用于人脸转正--Beyond Face Rotation
- 数据库基础知识六
- 参加CCF CSP认证者须知
- 把数组排成最小的数
- iOS设计模式之简单工厂模式
- xslt转换xml
- CSS样式之优先级
- 1016. Phone Bills (25)
- MongoDB高可用集群配置的几种方案
- tcpdump必杀技
- jQuery 语法
- 超小白教程之插入排序法