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.

对一组数字排序,使其组成的数字最大

思路:排序,不过重新定义排序规则,比如字符串x和字符串y,比较x+y和y+x的大小,如果x+y>y+x,则x>y

 class Solution:
def largestNumber(self, nums):
nums = [str(n) for n in nums]
nums.sort(cmp=lambda y,x:cmp(x+y,y+x))
return ''.join(nums).lstrip('') or ''

[leetcode sort]179. Largest Number的更多相关文章

  1. 【Leetcode】179. Largest Number

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

  2. 【刷题-LeetCode】179 Largest Number

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

  3. leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数

    这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...

  4. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

  5. [LeetCode] 179. Largest Number 最大组合数

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

  6. leetcode 179. Largest Number 求最大组合数 ---------- java

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

  7. Java for LeetCode 179 Largest Number

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

  8. [LeetCode] 179. Largest Number 解题思路

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

  9. [leetcode]179. Largest Number最大数

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

随机推荐

  1. 20155236 2016-2017-2 《Java程序设计》第五周学习总结

    20155236 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 Java的异常处理是通过5个关键字来实现的:try,catch,throw,throws,f ...

  2. flask跨域请求三行代码搞定

    flask跨域请求三行代码就可以搞定.但是请注意几点: 第一:只能返回json格式数据,比如list.ndarray等都不可以 第二:返回的对象必须是是字符串.元组.响应实例或WSGI可调用. pyt ...

  3. Django中html里的分页显示

    分页一(very low) 因为数据量过大,而又想直观便捷的查看数据,进而通过分页显示就可以完成这项工作 app中views.py LIST=[] #全局定义一个LIST for i in range ...

  4. centos7.2 安装 nginx

    Centos 7 源码编译安装 Nginx 1.13 原文地址:https://renwole.com/archives/39 1.先决条件: centos7.2 64位,安装配置nginx前必须安装 ...

  5. AutoCAD DevTV-AUTOCAD二次开发资源合集

    Webcast Language Date AutoCAD .Net - Session 2 English 13-Sep-12 AutoCAD .Net - Session 1 English 6- ...

  6. 如何编译和安装libevent【转】

    转自:http://www.open-open.com/lib/view/open1455522194089.html 来自: http://blog.csdn.net/yangzhenping/ar ...

  7. Windows 10安装MongoDB(安装&启动)

    Windows 10家庭中文版,MongoDB 3.6.3, 最近在学习Scrapy,可以却从未将scraped data存储到数据库中.在看过一些文档后,Scrapy会和MongoDB结合使用(还有 ...

  8. 组合比较符(PHP7+)

    php7+支持组合比较符,即<=>,英文叫做combined comparison operator,组合比较运算符可以轻松实现两个变量的比较,当然不仅限于数值类数据的比较. 语法:$a& ...

  9. python基础-类的反射

    1)反射是通过字符串方式映射内存中的对象. python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr, 改四个函数分别用于对对象内部执行:检查是 ...

  10. C# LINQ语句

    1.select 和 selectMany SelectMany() 将中间数组序列串联为一个最终结果值,其中包含每个中间数组中的每个值 2.join语句 from x in xx join d in ...