题目
题目来自leetcode
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.
思路
一个特殊的排序问题,解决方法如下,同样,java中也可以按这个思路来写
python代码
class Solution:
# @param num, a list of integers
# @return a string
def largestNumber(self, num):
if len(num) == 0:
return
for i in xrange(len(num)):
num[i] = str(num[i])
num.sort(reverse = True,cmp=self.sortbystr)
rs = ""
print num
for i in num:
rs += i
return rs if int(rs) != 0 else "0"
def sortbystr(self,a,b):
if a == b:
return 0
return 1 if str(a)+str(b) > str(b) + str(a) else -1
网友评论