Skip to content

Instantly share code, notes, and snippets.

@pdu
Created February 16, 2013 12:36
Show Gist options
  • Select an option

  • Save pdu/4966745 to your computer and use it in GitHub Desktop.

Select an option

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
#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