-
Notifications
You must be signed in to change notification settings - Fork 79
/
26.py
105 lines (83 loc) · 2.2 KB
/
26.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding:utf-8 -*-
import copy
def cmp(x, y):
if x > y:
return -1
else:
return 0
class Solution:
def Permutation(self, ss):
if ss is None:
return []
if len(ss) == 0:
return []
listss = list(ss)
listss.sort()
myList = []
myList.append(ss)
self.PermutationHelper(listss, myList)
return myList
def PermutationHelper2(self, ss):
if ss is None:
return []
if len(ss) == 0:
return []
mylist = []
mylist.append(ss)
size = len(ss)
while True:
p = len(ss) - 1
while p >= 1 and ss[p - 1] >= ss[p]:
p -= 1
if p == 0:
break
q = p
p -= 1
while q < size and ss[q] > ss[p]:
q += 1
q -= 1
ss = list(ss)
t = ss[p]
ss[p] = ss[q]
ss[q] = t
tempss = ss[p + 1:]
tempss = tempss[::-1]
ss = ss[0:p + 1] + tempss
ss = "".join(ss)
mylist.append(ss)
return mylist
def PermutationHelper(self, ss, Mylist):
size = len(ss)
for i in range(size - 2, -1, -1):
if ss[i] < ss[i + 1]:
flag = 1
for j in range(size - 1, i, -1):
if ss[j] > ss[i]:
t = ss[i]
ss[i] = ss[j]
ss[j] = t
tempss = ss[i + 1:]
tempss = tempss[::-1]
ss = ss[0:i + 1] + tempss
flag = 0
break
if flag == 0:
Mylist.append("".join(copy.copy(ss)))
self.PermutationHelper(ss, Mylist)
def reversedemo(ss):
return reversed(ss)
def demo():
ss = "122"
ss = list(ss)
mylist = []
mylist.append(copy.copy(ss))
s = Solution()
s.PermutationHelper(ss, mylist)
for items in mylist:
print items
# demo()
ss = '122'
s = Solution()
res = s.PermutationHelper2(ss)
for itemRes in res:
print itemRes