-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12e6f62
commit 5ac50b5
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |