Permutation Sequence
来源:互联网 发布:php html字符转义 编辑:程序博客网 时间:2024/05/22 08:09
解法一:最容易想到的就是求出所有的排列组合结果,然后找到第K个返回。
class Solution {public: bool flag[15]; char s[15]; string ans; int count = 0; void cal(int n, int index, int k) { if(count == k) return; if(index >= n) { count ++ ; if(count == k) ans = s; return; } for(int i = 1; i <= n; i ++) { if(flag[i] == false) { s[index] = i + '0'; flag[i] = true; cal(n, index + 1, k); flag[i] = false; } } } string getPermutation(int n, int k) { // Start typing your C/C++ solution below // DO NOT write int main() function memset(flag, 0, sizeof(flag)); memset(s, '\0', sizeof(s)); ans = ""; count = 0; cal(n, 0, k); return ans; }};
解法二:利用规律,逐个计算出结果中每一位应该是什么数字。假设结果是ans, 明显ans是一个n位的string,我们现在要做的就是把ans的每一位放入正确的字符。
对于ans的第index位,题目给定的求第K个,这时index后面还有 x = n - index - 1位,令 k_small = k / x!(取上整),第index位就应该是 n位数字中没有被选择过的第k_small小的数字, 同时k也需要更新 k -= (k_small - 1) * x! .
class Solution {public: bool flag[15]; char s[15]; int f[15]; int find_k_small(int n, int k_small) { int count = 0; for(int i = 1; i <= n; i ++) { if(flag[i] == false) {count ++ ;if(count == k_small) {flag[i] = true;return i;}} } return 0; } void cal(int n, int k) { int index = 0; int k_small; while(index < n) { int len = n - index - 1; k_small = k / f[len]; if(k % f[len] != 0) { k_small ++ ; } s[index] = find_k_small(n, k_small) + '0'; index ++ ; k -= (k_small - 1) * f[len]; } } string getPermutation(int n, int k) { // Start typing your C/C++ solution below // DO NOT write int main() function memset(flag, 0, sizeof(flag)); memset(s, '\0', sizeof(s)); f[0] = f[1] = 1; for(int i = 2; i < 10; i ++) f[i] = i * f[i-1]; cal(n, k); return string(s); }};
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Permutation Sequence
- Java 理论和实践: 安全构造技术
- NYOJ-8-一种排序(comparator排序)
- Httpclient 实现带参文件上传
- SlidingMenu动画效果的实现
- ubuntu黑屏解决办法
- Permutation Sequence
- iOS: NSArray的方法description
- 大道至简,职场上做人做事做管理
- Oracle数据库分区表原理学习
- 如何通过Aidl进行跨进程间的服务调用?
- python守护进程监控hive server(类形式)
- Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tool
- WIN7下VC6.0无法使用打开功能
- GDB命令小记