题解 | #把数组排成最小的数#

把数组排成最小的数

https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param numbers int整型一维数组 
# @return string字符串
#
import functools 
class Solution:
    def PrintMinNumber(self , numbers: List[int]) -> str:
        # write code here
        nums = list(map(str, numbers)) # 装成字符串
        nums = sorted(nums, key=functools.cmp_to_key(lambda x,y : 1 if x+y > y+x else -1 )) # key是我们的排序规则,内容即 若x+y>y+x那么x>y,也就是把能生成更小数的数放前边(x放y后边)
        return "".join(nums) # 输出这一串字符串

全部评论

相关推荐

巨人网络 测试 总包20左右
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务