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

前端 未结 16 2109
暖寄归人
暖寄归人 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:23

    Here is Python 3 solution where a is an array and n is the total number of element

    def check(p,q):
        p=str(p)
        q=str(q)
        d=max(p+q,q+p)
        if d==p+q:
            return int(p),int(q)
        else:
            return int(q),int(p)
    def find(a,n):
        for i in range(n):
            for j in range(n-1):
                c,d=check(a[j],a[j+1])
                a[j]=c
                a[j+1]=d
        print(*a,sep="")
    

提交回复
热议问题