把数组排成最小的数

题目描述:
输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
解题思路:
把a,b当做字符串拼接,若a+b < b+a,则a应该排在b前面。
因为python3中sort没有cmp这个关键字,而替代的key每回只能传入一个值。
即下面这样的写法是错误的。

#sorted相当于通过key获得新的元素进行比较,每次只传入一个元素
#key=str.lower这种写法是可以,比如说传入['A','B'],这相当于转化为'a','b',再进行排序
sorted(numbers,key=lambda x,y: int(x+y) -int(y+x))

所以可以使用下面这种方式写:

class Solution:
    def PrintMinNumber(self, numbers):
        key = cmp_to_key(lambda x, y: int(x +y) - int(y + x))
        res = ''.join(sorted(map(str, numbers), key=key)).lstrip('0')
        return res or ''

原因:
这是因为cmp_to_key的定义如下:

def cmp_to_key(mycmp):
    """Convert a cmp= function into a key= function"""
    class K(object):
        __slots__ = ['obj']
        def __init__(self, obj):
            self.obj = obj
        def __lt__(self, other):
            return mycmp(self.obj, other.obj) < 0
        def __gt__(self, other):
            return mycmp(self.obj, other.obj) > 0
        def __eq__(self, other):
            return mycmp(self.obj, other.obj) == 0
        def __le__(self, other):
            return mycmp(self.obj, other.obj) <= 0
        def __ge__(self, other):
            return mycmp(self.obj, other.obj) >= 0
        def __ne__(self, other):
            return mycmp(self.obj, other.obj) != 0
        __hash__ = None
    return K

我们相当于重写了mycmp,返回的是一个K的类,当让我们用K实例化后就可以进行比较,相当于我们重写了__lt__等方法,。
如下:

c = cmp_to_key(lambda x, y: int(x +y) - int(y + x))
print(c('3') < c('4') )  #True
print(c('3') > c('4') )  #False

当然我们也可以自己实现排序,下面使用了快排:

class Solution:
    def PrintMinNumber(self, numbers):
        res = ''.join(self.sorted(list(map(str, numbers)))).lstrip('0')
        return res or '0'
    def sorted(self,numbers):
        if len(numbers) == 0:
            return []
        x = numbers[0]
        return self.sorted([i for i in numbers[1:] if i+x < x+i ]) + [x] + self.sorted([i for i in numbers[1:] if i+x >= x+i])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值