/
Permutation Sequence.py
112 lines (106 loc) · 2.78 KB
/
Permutation Sequence.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
106
107
108
109
110
111
112
import math
class Solution:
# @param {integer} n
# @param {integer} k
# @return {string}
def getPermutation(self, n, k):
res = ''
k -= 1
nums = [str(i) for i in range(1, n+1)]
while n > 0:
tmp = math.factorial(n-1)
res += nums[k/tmp]
del nums[k/tmp]
k %= tmp
n -= 1
return res
# class Solution:
# def f(self,n,k):
# if n==1 :
# return [0]
# else:
# count=1
# for i in range(1,n):
# count*=i
# begin=(k-1)/count
# plus=k%count
# return [begin]+self.f(n-1,plus)
#
# # @return a string
# def getPermutation(self, n, k):
# res=self.f(n,k)
# print res
# lists=range(1,n+1)
# strs=''
# for i in range(n):
# strs+=str(lists[res[i]])
# lists.pop(res[i])
# return strs
if __name__=="__main__":
a=Solution()
print a.getPermutation(3, 1),"123"
print a.getPermutation(2,2)
print a.getPermutation(3,2)
#https://leetcode.com/discuss/16064/an-iterative-solution-for-reference
#TLE
# class Solution:
# def f(self,lists):
# if lists==None:
# return None
# tmpres=[]
#
# for idx,item in enumerate(lists):
# tmp=[i for i in lists]
# tmp.pop(idx)
# res=self.f(tmp)
# if len(res)>0:
# for i in res:
# tmpres.append(str(item)+i)
# else:
# tmpres.append(str(item))
# return tmpres
#
# # @return a string
# def getPermutation(self, n, k):
# if n==1:
# return '1'
# count=1
# begin=0
# plus=0
# for i in range(1,n):
# count*=i
# begin+=k/count
# plus=k%count
#
# tmp=[i for i in range(1,n+1)]
# if begin>0:
# tmp.pop(begin-1)
#
# tmp=self.f(tmp)
# if begin>0:
# return str(begin)+tmp[plus-1]
# else:
# return tmp[plus-1]
# TLE
# # class Solution:
# # def f(self,lists):
# # if lists==None:
# # return None
# # tmpres=[]
# #
# # for idx,item in enumerate(lists):
# # tmp=[i for i in lists]
# # tmp.pop(idx)
# # res=self.f(tmp)
# # if len(res)>0:
# # for i in res:
# # tmpres.append(str(item)+i)
# # else:
# # tmpres.append(str(item))
# # return tmpres
# #
# # # @return a string
# # def getPermutation(self, n, k):
# # tmp=self.f(range(1,n+1))
# # return tmp[k-1]
# #