2018-12-11 15:57:58 +08:00
|
|
|
''' mbinary
|
|
|
|
#########################################################################
|
|
|
|
# File : permute_divide_and_conquer.py
|
|
|
|
# Author: mbinary
|
|
|
|
# Mail: zhuheqin1@gmail.com
|
2019-01-31 12:09:46 +08:00
|
|
|
# Blog: https://mbinary.xyz
|
2018-12-11 15:57:58 +08:00
|
|
|
# Github: https://github.com/mbinary
|
|
|
|
# Created Time: 2018-11-25 12:23
|
|
|
|
# Description:
|
|
|
|
#########################################################################
|
|
|
|
'''
|
2020-01-01 15:51:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
def permute(lst, n):
|
2018-12-11 15:28:05 +08:00
|
|
|
''' O(n!), optimal'''
|
2020-01-01 15:51:32 +08:00
|
|
|
if n == 1:
|
|
|
|
print(lst)
|
2018-12-11 15:28:05 +08:00
|
|
|
else:
|
|
|
|
for i in range(n):
|
2020-01-01 15:51:32 +08:00
|
|
|
lst[i], lst[n-1] = lst[n-1], lst[i]
|
|
|
|
permute(lst, n-1)
|
|
|
|
lst[i], lst[n-1] = lst[n-1], lst[i]
|
|
|
|
|
2018-12-11 15:28:05 +08:00
|
|
|
|
2020-01-01 15:51:32 +08:00
|
|
|
if __name__ == '__main__':
|
2018-12-11 15:28:05 +08:00
|
|
|
n = 3
|
2020-01-01 15:51:32 +08:00
|
|
|
permute([i for i in range(n)], n)
|