leetcode largest number
2015-09-07 14:33
330 查看
视频分析:
http://v.youku.com/v_show/id_XMTMyOTM3NDcwMA==.html
题干:
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
思路分析:
这题论证方法严格来讲比较困难,不求甚解的话光写出代码来倒是不难。
http://v.youku.com/v_show/id_XMTMyOTM3NDcwMA==.html
题干:
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
思路分析:
这题论证方法严格来讲比较困难,不求甚解的话光写出代码来倒是不难。
[code]class Solution: # @param num, a list of integers # @return a string def largestNumber(self, num): num = sorted([str(x) for x in num], key = self.cmp_to_key(self.compare)) ans = ''.join(num).lstrip('0') return ans or '0' def compare(self, a, b): return [1, -1][a + b > b + a] def cmp_to_key(self,mycmp): class K: def __init__(self, obj, *args): self.obj = obj def __lt__(self, other): return mycmp(self.obj, other.obj) < 0 def __gt__(self, other): return mycmp(self.obj, other.obj) > 0 def __eq__(self, other): return mycmp(self.obj, other.obj) == 0 def __le__(self, other): return mycmp(self.obj, other.obj) <= 0 def __ge__(self, other): return mycmp(self.obj, other.obj) >= 0 def __ne__(self, other): return mycmp(self.obj, other.obj) != 0 return K
相关文章推荐
- VS2013更改nuget资源位置
- C++ Dynamic_cast
- 集群磁盘损坏导致hadoop文件丢失的概率计算
- ajax亦步刷新时,会导致某些javascript的失效解决方法
- 事件的标准用法
- 揭开HTTP网络协议神秘面纱系列(三)
- Angular JS入门教程(转)
- EqualsBuilder和HashCodeBuilder
- Web容器Jetty
- python爬虫糗事百科
- Java.sql.Date()和Java.util.Date()的互相转换
- <mvc:annotation-driven/>
- 一个很有趣的时间flash
- 按power key灭屏时,背光有一个慢慢变暗的过程,如果希望马上灭屏,应该如何修改?
- 生产者消费者模型(Linux系统下的两种实现方法)
- 跳转到AppStore下载
- hdu1166
- poj2236 并查集
- HTML相对路径和绝对路径
- poj 3281 最大流