解题思路:

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

题目简述: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.

解题思路:这有一种很简单的思路:

我们无非是要判断哪个串放在哪个的前面或者后面,这其实就是两个数的比较问题,只不过大小的比较方式不是通常的形式。当然通过字符串的处理有很多的方式,不过都略显复杂了,反正两个数的比较就两种情况,所以我们不妨列拿出来比较下得出结果就行。

#coding=utf-8

class Solution:

def cmp(self,x,y):

if x*(10**len(str(y)))+y < y*(10**len(str(x)))+x:

return 1

elif x*(10**len(str(y)))+y == y*(10**len(str(x)))+x:

return 0

else:

return -1

# @param num, a list of integers

# @return a string

def largestNumber(self, num):

num.sort(self.cmp)

return str(int(''.join(map(lambda x: str(x),num))))

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航