- 【LeetCode】剑指 Offer 45. 把数组排成最小的数
package offer; import java.util.Arrays; public class Solution45 { public static void main(String[] args) { int[] nums = {3,30,34,5,9}; Solution45 solution45 = new Solution45(); System.out.println(solution45.method(nums)); } private String method(int[] nums){ String[] strs = new String[nums.length]; for(int i = 0; i < nums.length; i++){ strs[i] = String.valueOf(nums[i]); } Arrays.sort(strs, (x, y) -> (x+y).compareTo(y+x)); StringBuilder res = new StringBuilder(); for(String s : strs){ res.append(s); } return res.toString(); } } //时间复杂度为 O(nlogn) //空间复杂度为 O(n)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)