Skip to content

Commit

Permalink
2023/7/10
Browse files Browse the repository at this point in the history
  • Loading branch information
KidJoe1412 committed Jul 10, 2023
1 parent 12e6f62 commit 5ac50b5
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/leetcode/Q179.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package leetcode;

import java.util.Arrays;

/**
* 题干:最大数
*给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数。
* 注意:输出结果可能非常大,所以你需要返回一个字符串而不是整数。
* ******************************************************
* 解法:贪心,用结果反推
* ******************************************************
* 原题:<a href="https://leetcode.cn/problems/largest-number/description/"></a>
* ******************************************************
*/
class Q179 {
public String largestNumber(int[] nums) {
int n = nums.length;
String[] s = new String[n];
for(int i = 0; i < n ;i++)
s[i] = nums[i] + "";
Arrays.sort(s,(a,b) -> {
String s1 = a + b,s2 = b + a;
return s1.compareTo(s2);
});
StringBuffer sb = new StringBuffer();
for(String str:s)
sb.append(str);
int index = 0; // 处理像[0,0,0]组成000这样的问题,这种需要返回0
while(index < n - 1 && sb.charAt(index) == '0')
index++;
return sb.toString().substring(index);
}
}

0 comments on commit 5ac50b5

Please sign in to comment.