码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode Largest Number python

时间:2015-06-14 08:12:00      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

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.

python code:

class Solution:
# @param num, a list of integers
# @return a string
def largestNumber(self, num):
  num=[str(x) for x in num]
  num.sort(cmp=lambda x, y: cmp(y+x, x+y))    #字符串按ascii排序即可
  return ‘‘.join(num).lstrip(‘0‘) or ‘0‘          #输出时不显示左边的0

leetcode Largest Number python

标签:

原文地址:http://www.cnblogs.com/bthl/p/4574532.html

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