8000 Create README.md · lixinsu/LeetCode@5eda7cd · GitHub
[go: up one dir, main page]

Skip to content 10000

Commit 5eda7cd

Browse files
committed
Create README.md
1 parent 79b2fd0 commit 5eda7cd

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

108. Permutation Sequence/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
首先,这个组合的顺序与 `std::next_permutation` 的顺序一致。可以参考 [排列组合技术](http://segmentfault.com/blog/pezy/1190000002486075)
2+
3+
4+
但如果这道题,直接使用 `std::next_permutation` 的话,会 TimeOut.
5+
6+
所以应该直接定位到 k 位置的字符串,而不是将所有组合都迭代一遍。
7+
8+
-----
9+
10+
需要两个索引表:
11+
12+
**table**
13+
14+
| n | 组合个数 |
15+
|:-:|:--------:|
16+
| 0 | 1 |
17+
| 1 | 1! |
18+
| 2 | 2! |
19+
| 3 | 3! |
20+
| 4 | 4! |
21+
| 5 | 5! |
22+
| 6 | 6! |
23+
| 7 | 7! |
24+
| 8 | 8! |
25+
| 9 | 9! |
26+
27+
**dictionary**
28+
29+
1 2 3 4 5 6 7 8 9
30+
31+
那么如果有了 k, 对于长度为 n 的字符串来说,首先要定位的是其开头字符。
32+
33+
int pos = (k-1)/table[n-1];
34+
ret += dict[pos];
35+
36+
出现过的字符,不应再有机会出现,故删除之。并继续查看后面的字符。同时 k 也要减掉之前计算过的 pos.
37+
38+
dict.earse(dict.begin() + pos);
39+
k = k - pos * table[n-1];

0 commit comments

Comments
 (0)
0