-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9fea76
commit e4d169f
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
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 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. | ||
Given k will be between 1 and n! inclusive. | ||
Example 1: | ||
Input: n = 3, k = 3 | ||
Output: "213" | ||
*/ | ||
class Solution { | ||
public: | ||
string getPermutation(int n, int k) { | ||
int fact[10]; | ||
fact[0]=fact[1]=1; | ||
for(int i=2;i<10;i++) | ||
fact[i]=i*fact[i-1]; | ||
string res="",set="123456789"; | ||
while(n>0){ | ||
int p=(k-1)/fact[n-1]; | ||
res+=set[p]; | ||
set.erase(set.begin()+p); | ||
k=k-p*fact[n-1]; | ||
n--; | ||
} | ||
return res; | ||
} | ||
}; |