File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -123,7 +123,7 @@ LeetCode C++ Solutions
123123| [ Unique Paths II] ( https://oj.leetcode.com/problems/unique-paths-ii/ ) | [ C++] ( ./src/uniquePaths/uniquePaths.II.cpp ) | Medium| yes|
124124| [ Unique Paths] ( https://oj.leetcode.com/problems/unique-paths/ ) | [ C++] ( ./src/uniquePaths/uniquePaths.cpp ) | Medium| yes|
125125| [ Rotate List] ( https://oj.leetcode.com/problems/rotate-list/ ) | [ C++] ( ./src/rotateList/rotateList.cpp ) | Medium| yes|
126- | [ Permutation Sequence] ( https://oj.leetcode.com/problems/permutation-sequence/ ) | [ C++] ( ./src/permutationSequence/permutationSequence.cpp ) | Medium|
126+ | [ Permutation Sequence] ( https://oj.leetcode.com/problems/permutation-sequence/ ) | [ C++] ( ./src/permutationSequence/permutationSequence.cpp ) | Medium| yes |
127127| [ Spiral Matrix II] ( https://oj.leetcode.com/problems/spiral-matrix-ii/ ) | [ C++] ( ./src/spiralMatrix/spiralMatrix.II.cpp ) | Medium| yes|
128128| [ Length of Last Word] ( https://oj.leetcode.com/problems/length-of-last-word/ ) | [ C++] ( ./src/lengthOfLastWord/lengthOfLastWord.cpp ) | Easy| yes|
129129| [ Insert Interval] ( https://oj.leetcode.com/problems/insert-interval/ ) | [ C++] ( ./src/insertInterval/insertInterval.cpp ) | Hard|
Original file line number Diff line number Diff line change 1+ #include < string>
2+ #include < iostream>
3+ #include < vector>
4+
5+ using namespace std ;
6+
7+ string getPermutation (int n, int k, string &str)
8+ {
9+ cout << " n=" << n << " k=" << k << " str=" << str << endl;
10+ if (k == 1 || str.length () == 1 )
11+ {
12+ return str;
13+ }
14+ int number = 1 ;
15+ for (int i=1 ; i<n; i++)
16+ {
17+ number *= i;
18+ }
19+ string result;
20+ int site = (k - 1 ) / number;
21+ result += str[site];
22+ str.erase (site, 1 );
23+
24+ if (site != 0 )
25+ {
26+ k = (k - 1 ) % number + 1 ;
27+ }
28+ result += getPermutation (n - 1 , k, str);
29+ return result;
30+ }
31+
32+ /* *
33+ * 采用递归方式实现,需要找到规律
34+ * 貌似该种算法就是康托编码
35+ */
36+ string getPermutation (int n, int k)
37+ {
38+ string str;
39+ if (n <= 0 && k < 0 )
40+ {
41+ return str;
42+ }
43+ for (int i=1 ; i<=n; i++)
44+ {
45+ str += (char )(i + ' 0' );
46+ }
47+
48+ return getPermutation (n, k, str);
49+ }
50+
51+ int main ()
52+ {
53+ cout << getPermutation (4 , 7 ) << endl;
54+ return 1 ;
55+ }
You can’t perform that action at this time.
0 commit comments