Created
February 16, 2013 12:36
-
-
Save pdu/4966745 to your computer and use it in GitHub Desktop.
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. http://leetcode.com/onlinejudge#question_60
This file contains hidden or 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
| #define MAXN 10 | |
| int num[MAXN]; | |
| class Solution { | |
| public: | |
| string getPermutation(int n, int k) { | |
| for (int i = 0; i < n; ++i) | |
| num[i] = i + 1; | |
| while (--k) | |
| next_permutation(num, num + n); | |
| stringstream is; | |
| for (int i = 0 ; i < n; ++i) | |
| is << num[i]; | |
| string ans; | |
| is >> ans; | |
| return ans; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment