91 lines
2.0 KiB
Java
91 lines
2.0 KiB
Java
//给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数。
|
||
//
|
||
// 注意:输出结果可能非常大,所以你需要返回一个字符串而不是整数。
|
||
//
|
||
//
|
||
//
|
||
// 示例 1:
|
||
//
|
||
//
|
||
//输入:nums = [10,2]
|
||
//输出:"210"
|
||
//
|
||
// 示例 2:
|
||
//
|
||
//
|
||
//输入:nums = [3,30,34,5,9]
|
||
//输出:"9534330"
|
||
//
|
||
//
|
||
// 示例 3:
|
||
//
|
||
//
|
||
//输入:nums = [1]
|
||
//输出:"1"
|
||
//
|
||
//
|
||
// 示例 4:
|
||
//
|
||
//
|
||
//输入:nums = [10]
|
||
//输出:"10"
|
||
//
|
||
//
|
||
//
|
||
//
|
||
// 提示:
|
||
//
|
||
//
|
||
// 1 <= nums.length <= 100
|
||
// 0 <= nums[i] <= 109
|
||
//
|
||
// Related Topics 排序
|
||
// 👍 542 👎 0
|
||
|
||
package leetcode.editor.cn;
|
||
|
||
import java.util.Arrays;
|
||
|
||
//179:最大数
|
||
public class LargestNumber {
|
||
public static void main(String[] args) {
|
||
//测试代码
|
||
Solution solution = new LargestNumber().new Solution();
|
||
solution.largestNumber(new int[]{10, 2});
|
||
}
|
||
|
||
//力扣代码
|
||
//leetcode submit region begin(Prohibit modification and deletion)
|
||
class Solution {
|
||
public String largestNumber(int[] nums) {
|
||
int n = nums.length;
|
||
// 转换成包装类型,以便传入 Comparator 对象(此处为 lambda 表达式)
|
||
Integer[] numsArr = new Integer[n];
|
||
for (int i = 0; i < n; i++) {
|
||
numsArr[i] = nums[i];
|
||
}
|
||
|
||
Arrays.sort(numsArr, (x, y) -> {
|
||
long sx = 10, sy = 10;
|
||
while (sx <= x) {
|
||
sx *= 10;
|
||
}
|
||
while (sy <= y) {
|
||
sy *= 10;
|
||
}
|
||
return (int) (-sy * x - y + sx * y + x);
|
||
});
|
||
|
||
if (numsArr[0] == 0) {
|
||
return "0";
|
||
}
|
||
StringBuilder ret = new StringBuilder();
|
||
for (int num : numsArr) {
|
||
ret.append(num);
|
||
}
|
||
return ret.toString();
|
||
}
|
||
}
|
||
//leetcode submit region end(Prohibit modification and deletion)
|
||
|
||
} |