How can I manipulate an array to make the largest number?

前端 未结 16 2327
暖寄归人
暖寄归人 2021-01-30 02:47

Say you have an array of positive integers, manipulate them so that the concatenation of the integers of the resultant array is the largest number possible. Ex: {9,1,95,17,5}, r

16条回答
  •  情话喂你
    2021-01-30 03:14

    I guess it has already been solved. Here are few lines of code in Python using the logic already discussed in few of the answers:

    >>li = [9,1,95,17,5]
    >>li.sort(cmp=lambda a,b: cmp(int(str(a)+str(b)), int(str(b) + str(a))), reverse=True)
    
    >>output = ""
    >>for i in li:
    output += str(i)
    
    >>print  output
    

提交回复
热议问题