[LeetCode]60. Permutation Sequence
来源:互联网 发布:浙江大学软件学院硕士 编辑:程序博客网 时间:2024/05/01 05:43
60. Permutation Sequence
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.
分析
寻找[1,...,n]
的第k
个组合可以通过[LeetCode 31. Next Permutation]来解决,只要对一个原始序列迭代k-1
次即可得到结果,但是这个方法在LeetCode上提交后出现超时现象,因此需要寻找其他方法。
对于一个序列p(a1,a2,...,an)
,a1
出现在第一个位置的组合有(n-1)!
个,那么第k
个组合的首位数字一定是p序列的第k/(n-1)!
个数,以此类推得出:
a1 = k1 / (n-1)!
k2 = k1 % (n-1)!
a2 = k2/(n-2)!
kn = k(n-1)% 1!
an = k(n) / 0!
源码
方法1:通过next permutation迭代k-1
次
class Solution {public: string getPermutation(int n, int k) { vector<int> nums(n); for(int i = 0; i < n; i++){ nums[i] = (i + 1); } for(int j = 0; j < k - 1; j++) { nextPermutation(nums); } string ret; for(int m = 0; m < n; m++) { ret.push_back('0' + nums[m]); } return ret; } void nextPermutation(vector<int>& nums) { if(nums.size() < 2) return; int i = nums.size() - 1; int j = -1; // 1. 反向寻找到的pivot位置 while(i > 0) { if(nums[i] > nums[i - 1]) { // 后一个数 < 前一个数 j = i - 1; break; } i--; } if(j == -1) { // 说明序列是降序,只要翻转序列即可 reverse(nums, 0, nums.size()); } else { // 2. 序列存在下一个更大的排列,在nums.size() - 1,...,j+1间找到第一个比nums[j]要大的数 int k = nums.size() - 1; while(k > j) { if(nums[k] > nums[j]) break; k--; } //3. 找到了交换的位置为k swap(nums[j], nums[k]); //4. 翻转交换后的序列 reverse(nums,j + 1, nums.size() - j - 1); } } // 翻转一个序列 void reverse(vector<int>& nums, int start, int length) { int middle = length / 2; int i = 0; while(i < middle) { swap(nums[start + i], nums[start + (length - 1 - i)]); i++; } } // 不占用额外空间交换两个数 void swap(int& a, int& b) { a = a + b; b = a - b; a = a - b; }};
方法二:
string getPermutation(int n, int k) { vector<int> nums(n); long long factorial = 1; for(int i = 0; i < n; i++) { // 计算阶乘 factorial *=(i + 1); nums[i] = (i + 1); } string ret; k--; int found = 0; for(int j = 0; j < n; j++) { factorial = factorial / (n - j); // 从(n - 1)!开始 found = k / factorial; ret.push_back('0' + nums[found]); nums.erase(nums.begin() + found);//将找到的数字从序列中删除 k = k % factorial; } return ret; }
0 0
- LeetCode --- 60. Permutation Sequence
- [Leetcode] 60. Permutation Sequence
- [leetcode] 60.Permutation Sequence
- **LeetCode 60. Permutation Sequence
- leetcode 60. Permutation Sequence
- [LeetCode]60. Permutation Sequence
- LeetCode *** 60. Permutation Sequence
- LeetCode 60. Permutation Sequence
- leetcode 60. Permutation Sequence
- LeetCode 60. Permutation Sequence
- leetcode:60. Permutation Sequence
- [Leetcode] 60. Permutation Sequence
- [leetcode] 60. Permutation Sequence
- [LeetCode] 60. Permutation Sequence
- leetcode 60. Permutation Sequence
- Leetcode-60. Permutation Sequence
- [LeetCode]--60. Permutation Sequence
- leetcode-60. Permutation Sequence
- Intel Edison上使用jasoncpp
- 80x86指令/二进制码对照表
- 数据结构--链队列基本操作
- JS实现图片懒加载
- uboot下用SecureCRT脚本实现一键烧写linux
- [LeetCode]60. Permutation Sequence
- 【BZOJ1013】球形空间产生器sphere 高斯消元
- hdu 3010
- 3.1.3 Memcached的安装(2)
- 内部类
- UVA 1615 Highway 区间选点 经典贪心 (SEERC 2005)
- uboot必须解决哪些问题
- An Introduction to Machine Learning with Python
- Linux下配置用msmtp和mutt发邮件