Leetcode--largest number

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.