def sort_char_list(lst):
alphabet = "abcdefghijklmnopqrstuvwxyz"
# Here, create the 26 buckets
new_list = [''] * len(alphabet)
for letter in lst:
# This is the bucket index
# You could use `ord(letter) - ord('a')` in this specific case, but it is not mandatory
index = alphabet.index(letter)
new_list[index] += letter
# Assemble the buckets
return ''.join(new_list)
你在找桶型的。此处:
至于复杂性,由于
alphabet
是一个预定义的固定大小字符串,因此在其中搜索一个字母最多需要26个操作,即O(1)
。因此,总体复杂性是O(n)
相关问题 更多 >
编程相关推荐