ID | Title | Difficulty | |
---|---|---|---|
Loading... |
179. Largest Number
Medium
LeetCode
Array, String, Greedy, Sorting
Problem
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
Input: [10,2]
Output: "210"
Example 2:
Input: [3,30,34,5,9]
Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer.
Code
class Solution {
public String largestNumber(int[] num) {
if(num == null || num.length == 0)
return "";
String[] s_num = new String[num.length];
for(int i = 0; i < num.length; i++){
s_num[i] = String.valueOf(num[i]);
}
Comparator<String> comp = new Comparator<String>(){
@Override
public int compare(String str1, String str2){
String s1 = str1 + str2;
String s2 = str2 + str1;
return s2.compareTo(s1);
}
};
Arrays.sort(s_num, comp);
if(s_num[0].equals("0")) return "0";
StringBuilder sb = new StringBuilder();
for(String s: s_num){
sb.append(s);
}
return sb.toString();
}
}
按 <- 键看上一题!
178. Rank Scores
按 -> 键看下一题!
180. Consecutive Numbers