【leetcode】Largest Number

题目简述:

Given a list of non negative integers, arrange them such that they form the largest number.code

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.orm

Note: The result may be very large, so you need to return a string instead of an integer.utf-8

解题思路:

这有一种很简单的思路:
咱们无非是要判断哪一个串放在哪一个的前面或者后面,这其实就是两个数的比较问题,只不过大小的比较方式不是一般的形式。固然经过字符串的处理有不少的方式,不过都略显复杂了,反正两个数的比较就两种状况,因此咱们不妨列拿出来比较下得出结果就行。字符串

#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))))
相关文章
相关标签/搜索