forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation_Sequence.py
More file actions
37 lines (32 loc) · 866 Bytes
/
Permutation_Sequence.py
File metadata and controls
37 lines (32 loc) · 866 Bytes
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
"""
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
"""
class Solution:
# @return a string
def getPermutation(self, n, k):
num_list = []
total = 1
res = ''
for i in range(1, n+1): # Detail!!! this is n+1
total *= i
num_list.append(str(i))
k -= 1 # This is very important
while n > 0:
total /= n
i = k / total
k %= total
res += num_list[i]
num_list.pop(i)
n -= 1
return res
# total is very important here