题解 | #最大数# 不使用字符串拼接

最大数

http://www.nowcoder.com/practice/fc897457408f4bbe9d3f87588f497729

采用冒泡排序,分情况比较

# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 最大数
# @param nums int整型一维数组 
# @return string字符串
#
class Solution:
    def solve(self , nums: List[int]) -> str:
        # write code here
        if all([x == 0 for x in nums]):
            return '0'
        s = [str(i) for i in nums]
        n = len(nums)
        for i in range(n):
            for j in range(n-i-1):
                x = s[j]
                y = s[j+1]
                l1 = len(x)
                l2 = len(y)
                if l1 == l2:
                    if x < y:
                       s[j], s[j+1] = s[j+1], s[j]
                else:
                    u, v = 0, 0
                    while u < l1 and v < l2 and x[u] == y[v]:
                        u += 1
                        v += 1
                    if u < l1 and v < l2: #[110, 12]
                        if x[u] < y[v]:
                            s[j], s[j+1] = s[j+1], s[j]

                    elif u < l1:  #[120, 12]
                        if x[u] < y[0]:
                            s[j], s[j+1] = s[j+1], s[j]

                    elif v < l2:  #[12, 122]
                        if x[0] < y[v]:
                            s[j], s[j+1] = s[j+1], s[j]

        return ''.join(s)
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务