2017-08-14 69 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"