1. 程式人生 > >劍指offer 字串的排列 Python實現

劍指offer 字串的排列 Python實現

def list_all(string):
    new_list_set = []
    if len(string) == 1:
        return string
    for i in range(len(string)):
        sub_string = string[:]
        sub_string.pop(i)
        result_list = list_all(sub_string)
        for result in result_list:
            if result+string[i] not in new_list_set:
                new_list_set.append(result+string[i])
    return new_list_set


string = list("asa")
for string_list in list_all(string):
    print(string_list)
將陣列的全排列看成是通過遞迴實現的,則只要將每次去除元素string[i]後的子串遞迴即可