1. 程式人生 > >Leetcode 179. Largest Number (Medium) (java)

Leetcode 179. Largest Number (Medium) (java)

Leetcode 179. Largest Number (Medium) (java)

Tag: Sort

Difficulty: Medium

/*

179. Largest Number (Medium)

Given a list of non negative integers, arrange them such that they form the largest number.

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

*/
public 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>(){
			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].charAt(0) == '0') return "0";
		StringBuilder sb = new StringBuilder();
		for(String s: s_num)
			sb.append(s);
		return sb.toString();
	}
}


相關推薦

Leetcode 179. Largest Number (Medium) (java)

Leetcode 179. Largest Number (Medium) (java) Tag: Sort Difficulty: Medium /* 179. Largest Numbe

LeetCode 179.Largest Number (最大數)

題目描述: 給定一組非負整數,重新排列它們的順序使之組成一個最大的整數。 示例 1: 輸入: [10,2] 輸出: 210 示例 2: 輸入: [3,30,34,5,9] 輸出: 9534330 說明: 輸出結果可能非常大,所以你需要返回一個字串而不是

#Leetcode# 179. Largest Number

https://leetcode.com/problems/largest-number/   Given a list of non negative integers, arrange them such that they form the largest number. Example

python leetcode 179. Largest Number

基本思路是“大”的數排前面,比如’9’>‘87’ 再者是‘42’和‘420’的比較,‘42420’>‘42042’,如果只是單純地在’42’後加’2’變成442>420,這樣會遇到另外一個問題就是‘42’和‘422’的比較,‘42422’>‘42242’。pyth

演算法設計與分析課作業【week8】leetcode--179. Largest Number

題目 Given a list of non negative integers, arrange them such that they form the largest number. Example 1: Input: [10,2] Output: "210"

#leetcode#179. Largest Number

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the large

LeetCode 179. Largest Number(最大數)

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the

leetcode 179: Largest Number

Largest Number Total Accepted: 2269 Total Submissions: 15366 Given a list of non negative integers, arrange them such that they form

[Lintcode]184. Largest Number/[Leetcode]179. Largest Number

cti code sele des form 參考 ace nlog map 184. Largest Number/179. Largest Number 本題難度: Medium Topic: Greedy Description Largest Number 中

Leetcode179. Largest Number

class 數組 new 需要 for ++ ret instead app Given a list of non negative integers, arrange them such that they form the largest number. For ex

Learning python by leetcode: No.179 Largest number

contents 題目 algorithm code details \_\_lt\_\_() map(function, iterable) str.join(iterable) sorted(iterab

179. Largest Number

number public integer array ons con app complex str1 Given a list of non negative integers, arrange them such that they form the largest

leetcode-747-Largest Number At Least Twice of Others(求vector的最大值和次大值)

mis bmi 函數 ret num 容易 浪費 ever leetcode 題目描述: In a given integer array nums, there is always exactly one largest element. Find whether the

179.Largest Number

一、題目   1、審題      2、分析     給出一個整形陣列,將其中的元素進行拼接,形成的最大的數值。返回字串形式的這個數值(防止溢位)。   二、解答   1、思路:     ①、將整形陣列轉為字串陣列;     ②、重寫 Comparator 的 compar

leetcodeLargest Number At Least Twice of Others)

Title:Largest Number At Least Twice of Others   747 Difficulty:Easy 原題leetcode地址:https://leetcode.com/problems/largest-number-at-least-twic

LeetCode---(197)Largest Number

Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the l

179 Largest Number

題目: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34

LEETCODE 9 Palindrome NumberJAVA題解)

https://leetcode.com/problems/palindrome-number/ 原題如上。 題意解析: 判斷一個整數是否是迴文整數。像123321,,121,這樣的,就是迴文整數

179. Largest Number 排成最大的數字(重要!)

https://leetcode.com/problems/largest-number/ Given a list of non negative integers, arrange them such that they form the largest numb

[C++]LeetCode: 128 Largest Number (自定義比較函式排序)

題目: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9],