GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — CodingInterview/Solution45.java at master · legolas007/CodingInterview Expand file tree/ Copy pathSolution45.javaMore file actions 40 lines (38 loc) · 1.26 KB / Copy pathSolution45.javaFile metadata and controls40 lines (38 loc) · 1.26 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.usher.algorithm.offer; import java.lang.reflect.Array; import java.util.Arrays; import java.util.Comparator; /** * @Author: Usher * @Description: 先将整型数组转换成String数组,然后将String数组排序,最后将排好序的字符串数组拼接出来。关键就是制定排序规则。 * 排序规则如下: * 若ab > ba 则 a > b, * 若ab < ba 则 a < b, * 若ab = ba 则 a = b; * 解释说明: * 比如 "3" < "31"但是 "331" > "313",所以要将二者拼接起来进行比较 */ public class Solution45 { public String PrintMinNumber(int [] numbers) { int len = numbers.length; StringBuffer stringBuffer = new StringBuffer(); //String[]字符串数组 String[] strings = new String[len]; for(int i=0;i<len;i++){ strings[i] = String.valueOf(numbers[i]); } Arrays.sort(strings, new Comparator<String>() { @Override public int compare(String o1, String o2) { String s1 = o1 + o2; String s2 = o2+o1; return s1.compareTo(s2); } }); for (int i=0;i<len;i++){ stringBuffer.append(strings[i]); } return stringBuffer.toString(); } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |