Description:
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 is very large, so you need to return a string instead of an integer.
The number of the given to the largest number, the first thought of the idea is a bitwise comparison, but that is more troublesome and prone to bugs, so there is a simple idea is to compare the two situations in the order of the results of the largest sequence. Code less well understood.
public class Solution {public String largestnumber (int[] nums) { arraylist<number> list = new Arraylist< ; Number> (); for (int i:nums) { List.add (new number (i+ "")); } Collections.sort (list); StringBuilder sb = new StringBuilder (); for (int i=list.size ()-1; i>=0; i--) { sb.append (List.get (i). val); } Java.math.BigInteger B = New Java.math.BigInteger (sb.tostring ()); return b.tostring (); }} Class Number implements Comparable<number> { String val; Public number (String val) { this.val = val; } public int CompareTo (number N) { Java.math.BigInteger a = new Java.math.BigInteger (This.val + n.val); Java.math.BigInteger B = New Java.math.BigInteger (N.val + this.val); return A.compareto (b); }}
Leetcode--largest number