题解 | #最大数#
最大数
https://www.nowcoder.com/practice/fc897457408f4bbe9d3f87588f497729
分治法,排序再组合
import java.util.*; public class Solution { /** * 最大数 * @param nums int整型一维数组 * @return string字符串 */ public static String solve (int[] nums) { StringBuilder res = new StringBuilder(); def(nums,0,nums.length-1); if(nums[0]==0) { return "0"; } for(int i=0;i<nums.length;i++) { res.append(nums[i]); } return res.toString(); } private static void def(int[] nums, int left, int right) { if(left < right){ int i = left; int j = right; int x = nums[i]; while(i < j ) { while(i < j && compair(nums[j],x)) { j--; } if(i < j) { nums[i] = nums[j]; i++; } while(i < j && compair(x,nums[i])) { i++; } if(i < j) { nums[j] = nums[i]; j--; } } nums[i] = x; def(nums,left,i-1); def(nums,i+1,right); } } public static boolean compair(int x,int y) {//xy是否大于yx int xlen = 10; int ylen = 10; int xcopy = x; int ycopy = y; while(xcopy>=10) { xcopy = xcopy/10; xlen *= 10; } while(ycopy>=10) { ycopy = ycopy/10; ylen *= 10; } return (y * xlen + x) > (x * ylen + y); } }