[leetcode sort]179. Largest Number
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的更多相关文章
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【刷题-LeetCode】179 Largest Number
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- [LeetCode] 179. Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. Example ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [leetcode]179. Largest Number最大数
Given a list of non negative integers, arrange them such that they form the largest number. Input: [ ...
随机推荐
- 网摘关于BarCodeControl控件
简介 BarCodeControl是一个用户制作条形码的控件. MicrosoftBarcodeControl9.0是可以在MicrosoftOfficeAccess窗体和报表中显示条码符号的Acti ...
- Java容器List接口
List接口是Java中经常用到的接口,如果对具体的List实现类的特性不了解的话,可能会导致程序性能的下降,下面从原理上简单的介绍List的具体实现: 可以看到,List继承了Collection接 ...
- [CodePlus 2017 11月赛&洛谷P4058]木材 题解(二分答案)
[CodePlus 2017 11月赛&洛谷P4058]木材 Description 有 n棵树,初始时每棵树的高度为 Hi ,第 i棵树每月都会长高 Ai.现在有个木料长度总量为 S的订单, ...
- HAOI 2005 路由选择问题 (最短路+次短路)
问题描述 X城有一个含有N个节点的通信网络,在通信中,我们往往关心信息从一个节点I传输到节点J的最短路径.遗憾的是,由于种种原因,线路中总有一些节点会出故障,因此在传输中要避开故障节点. 任务一:在己 ...
- python学习之argparse模块的使用
以下内容主要来自:http://wiki.jikexueyuan.com/project/explore-python/Standard-Modules/argparse.html argparse ...
- asp.net mvc发送邮件
参考文献: 第一篇:http://www.cnblogs.com/qinpengming/archive/2011/06/08/2075040.html 第二篇:http://www.cnblogs. ...
- RobotFramework安装扩展库包Selenium2Library(三)
Robot Framework扩展库包 http://robotframework.org/#libraries 一,自动化测试web端 1,pip安装SeleniumLibrary pip inst ...
- cvpr densnet论文
- 原生js封装dom操作库
var utils = (function(window) { var flag = "getComputedStyle" in window; function win(attr ...
- Unix IPC之Posix消息队列(2)
/* Query status and attributes of message queue MQDES. */ extern int mq_getattr (mqd_t __mqdes, stru ...