码迷,mamicode.com
首页 > 其他好文 > 详细

179. Largest Number(sort中比较规则的用法)

时间:2018-10-04 09:27:23      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:string   code   return   比较   str   sel   中比   and   sorted   

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.

class compare(str):
    def __lt__(a, b):
        return a + b >b + a

class 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(sort中比较规则的用法)

标签:string   code   return   比较   str   sel   中比   and   sorted   

原文地址:https://www.cnblogs.com/bernieloveslife/p/9739731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!