179. Largest number (use of comparison rules in sort)

Source: Internet
Author: User

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 is very large, so you need to return a string instead of an integer.

class compare(str):    def __lt__(a, b):        return a + b >b + aclass Solution(object):    def largestNumber(self, nums):        """        :type nums: List[int]        :rtype: str        """        res = ‘‘.join(sorted(map(str,nums),key = compare))        return ‘0‘ if res[0]==‘0‘ else res

Konw How to use compare and map

179. Largest number (use of comparison rules in sort)

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.