-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.py
64 lines (54 loc) · 1.36 KB
/
lex.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
# coding=utf-8
##字典序全排列算法
LIMIT = 1000000
def lex(n):
seed = []
for i in range(n):
seed.append(i)
count = 1
while True:
#print seed
count +=1
j = 0
m = 0
indexs = range(1,n)
indexs.reverse()
#print indexs
#从右向左遍历遍历,找到第一个减小的值的索引
#print seed
for i in indexs:
#print seed[i-1],seed[i]
if seed[i-1]<seed[i]:
j = i-1
#print j
break
#从右向左遍历,找到第一个比seed[j]大的数的索引
#indexs = indexs.insert(9,0)
indexs.append(0)
#print 'indexs',indexs
for i in indexs:
#print seed[i]
if seed[i]>seed[j]:
#print i
m = i
break
# print m
#交换seed[j]和seed[m]
#print j,m
#print seed
seed[j],seed[m] =seed[m],seed[j]
#print 'seed',seed
#将seed[j]之后的元素进行reverse
tmp = seed[j+1:]
#print 'tmp',tmp
tmp.reverse()
#print tmp
b = seed[:j+1]
b.extend(tmp)
seed = b
#print seed
if count == LIMIT:
print seed
break
lex(10)