题解 | #最大数#
最大数
http://www.nowcoder.com/practice/fc897457408f4bbe9d3f87588f497729
python3解法
利用现有库将conpare转为key
from functools import cmp_to_key
python2
- 修改比较大小的计算方法,例如ab拼接后的大小关系,逆序所以是ba-ab
- 考虑数组含有多个0的特殊情况,如果nums长度大于1,那么第一个数不能是0
#
# 最大数
# @param nums int整型一维数组
# @return string字符串
#
def cmp(a, b):
ab = int(str(a) + str(b))
ba = int(str(b) + str(a))
return ba - ab
class Solution:
def solve(self , nums ):
# write code here
nums = sorted(nums, cmp)
while len(nums) > 1 and nums[0] == 0:
nums.pop(0)
continue
return ''.join(map(str, nums))
查看10道真题和解析
