题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

words = input().strip().split()

lengths = []
alphas = []
others = []
for word in words:
    lengths.append(len(word))
    for w in word:
        if not w.isalpha():
            others.append((len(alphas) + len(others), w))
        else:
            alphas.append(w)


def insert_sort(val: list[str]):
    for i in range(len(val)):
        if not val[i].isalpha():
            continue

        j = i - 1
        cur = val[i]
        while j >= 0:
            if not val[j].isalpha():
                j -= 1
                continue
            if cur == val[j] or cur.lower() == val[j].lower():
                if not val[j + 1].isalpha():
                    val[j + 2] = cur
                else:
                    val[j + 1] = cur
                break
            elif cur.lower() < val[j].lower():
                if not val[j + 1].isalpha():
                    val[j + 2] = val[j]
                else:
                    val[j + 1] = val[j]
                j -= 1
            elif cur.lower() > val[j].lower():
                val[j + 1] = cur
                break
        if cur.lower() < val[0].lower():
            val[0] = cur


insert_sort(alphas)

for other in others:
    alphas.insert(other[0], other[1])

cur = 0
for l in lengths:
    print("".join(alphas[cur : l + cur]), end=" ")
    cur += l

全部评论

相关推荐

08-05 14:05
门头沟学院 Java
Twilight_m...:你直接问他,马总能不能直聘让我进阿里
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务