gpt4 book ai didi

python - 在不使用任何内置 python 排序函数的情况下有效地对仅包含 0 和 1 的列表进行排序?

转载 作者:行者123 更新时间:2023-11-28 19:33:33 25 4
gpt4 key购买 nike

什么是最有效的排序列表的方法,[0,0,1,0,1,1,0] 的元素只有 0 & 1,不使用任何内置的 sort()sorted()count() 函数。 O(n) 或更小

最佳答案

>>> lst = [0,0,1,0,1,1,0]
>>> l, s = len(lst), sum(lst)
>>> result = [0] * (l - s) + [1] * s
>>> result
[0, 0, 0, 0, 1, 1, 1]

关于python - 在不使用任何内置 python 排序函数的情况下有效地对仅包含 0 和 1 的列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11175645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com