-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathstringunicode.py
More file actions
37 lines (32 loc) · 1.06 KB
/
Copy pathstringunicode.py
File metadata and controls
37 lines (32 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# coding: utf8
# @Author: 郭 璞
# @File: 字符串全排列.py
# @Time: 2017/4/3
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 无重复的字母组成的不同的串
def entrance(s):
if len(s)==0:
return;
result = []
tempset = set([])
digui(s, tempset, 0)
result.extend(tempset)
return result
def digui(chars, strset, position):
# 递归终止条件, 一旦position到达倒数第二个位置即可停止
if position == len(chars)-1:
strset.add("".join(chars))
else:
for index in range(position, len(chars)):
# 交换与position相邻的元素
chars[position], chars[index] = chars[index], chars[position]
print(chars)
# 递归到下一个位置
digui(chars, strset, position+1)
# 记得交换回来
chars[index], chars[position] = chars[position], chars[index]
if __name__ == '__main__':
s = ['a', 'b', 'c']
result = entrance(s)
print(result)