2017-08-14 93 views
-4

问题: 给定一个非负数整数列表,将它们排列成最大数字。列表中最大的数字python

所以给出[1,20,23,4,8,最大的形成数量为8423201.

我就是看不惯以下解决方案:

是什么num.sort(cmp=lambda x, y: cmp(y + x, x + y))办?

为什么它有两个参数x和y?如果输入一个列表,x和y在列表中表示什么?

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)) 
     largest = ''.join(num) 
     return largest.lstrip('0') or '0' 

if __name__ == "__main__": 
    num = [3, 30, 34, 5, 9] 
    print Solution().largestNumber(num) 

有人可以解释代码解决方案吗?谢谢。

+0

FWIW,该代码将只对Python的2.使用'cmp'功能参数'sort'的工作已被弃用,在Python 3不再存在,看到的是https:/ /stackoverflow.com/questions/30140796/sort-a-list-to-form-the-largest-possible-number对这个问题的各种解决方案。您可以阅读[文档](https://docs.python.org/2/library/stdtypes.html#mutable-sequence-types)中的'sort'中的'cmp',请参阅注释8。 docs for the built-in ['cmp'](https://docs.python.org/2/library/functions.html#cmp)。 –

+0

这是使用旧式比较函数来排序列表,它使用字符串连接和比较来排序,例如, ''3'+'30'(330)>'30'+'3'(303)',因为“8XX”>“XX8”'',所以会强制列表前面的'8'。 – AChampion

回答

-5

list.sort(function)用你指示的function来整理你的清单。

0

下面是对代码的解释。

class Solution: 
    # @param num, a list of integers 
    # @return a string 
    def largestNumber(self, num): 
     # converts list of numbers to list of strings 
     num = [str(x) for x in num] 
     # sorts list based on comparator function provided (an "anonymous" lambda function) 
     # the comparator is used to compare pairs of elements for sorting (x comes first returns -1, "equal" returns 0, x comes after returns 1) 
     # the lambda function takes two arguments x and y because comparator takes two arguments 
     # since these are strings, they are compared by seeing which concatenation (+ operator with strings) 
     # comes "first" alphabetically but note the x and y are "swapped" from what you might expect (to reverse the sort) 
     num.sort(cmp=lambda x, y: cmp(y + x, x + y)) 
     # join the sorted list together with empty string separators 
     largest = ''.join(num) 
     # remove any leading 0s 
     return largest.lstrip('0') or '0' 

if __name__ == "__main__": 
    num = [3, 30, 34, 5, 9] 
    print Solution().largestNumber(num) 
0

Python 2.x排序功能允许用于比较两个项目的cmp函数。 cmp(a, b)如果a < b返回-1,如果a == b则返回0,如果a> b则返回1。

此代码使用cmp“创造性地”得到所需的排序顺序来解决问题;它将在“80”之前排序“8”,因为“880”>“808”。

问题是,你想要一个反向字母排序,但是你想要长字符串之前的短字符串(如果它们的前缀是相同的)。

更通用的解决方案是按字母顺序进行反向排序,但将所有字符串右键填充到相同长度 - 至少与您排序的最长字符串一样长 - 将字符排序为“大于9 ”。

我们如何选择这样的角色?那么,

ord("9")           # -> 57 
print("".join(chr(ch) for ch in range(57, 75)) # "9:;<=>[email protected]" 

所以"A"看起来像一个不错的选择,而且很容易记住,因为它在十六进制的10。

然后

def largest_number(nums): 
    # convert to strings 
    nums = [str(i) for i in nums] 

    # find the length of the longest string 
    longest = max(len(s) for s in nums) 

    # create a function to pad strings to that length 
    def padded(s, pad_char="A", length=longest): 
     return s + pad_char * (length - len(s)) 

    # sort the strings greatest-to-least according to their padded values 
    nums.sort(key=padded, reverse=True) 
    # nums is now ["9", "5", "3", "34", "30"] 

    # get the resulting output-string 
    num = "".join(nums) 

    # remove any leading 0s 
    # (this should only ever occur if all input nums were 0) 
    num = num.lstrip("0") 

    if num: 
     return num 
    else: 
     # the string was all 0s - return a single 0 
     return "0" 

if __name__ == "__main__": 
    nums = [3, 30, 34, 5, 9] 
    print(largest_number(nums)) # -> "9533430"