[LeetCode] Next Permutation、Permutations、Permutations II、Permutation Sequence
来源:互联网 发布:安卓优化清理大师 编辑:程序博客网 时间:2024/06/05 03:40
Next Permutation:
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.1,2,3
→ 1,3,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,1
class Solution {public: void nextPermutation(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function int n=num.size(); if ( n<=1) return; int p=n-2; while(p>=0&&num[p]>=num[p+1]) p--; if ( p<0 ) { sort(num.begin(),num.end()); return; } int k=p+1; while(k<n&&num[k]>num[p]) k++; k--; swap(num[p],num[k]); sort(&num[p+1],&num[n]); return; }};
Permutations:
Given a collection of numbers, return all possible permutations.
For example,[1,2,3]
have the following permutations:[1,2,3]
, [1,3,2]
, [2,1,3]
, [2,3,1]
, [3,1,2]
, and [3,2,1]
class Solution {public: vector<vector<int> > permute(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > ans; solve(ans,num,0); return ans; } void solve(vector<vector<int> >& ans, vector<int>& num,int k) { int n =num.size(); if ( k>=n ) { ans.push_back(num); return; } for(int i=k;i<n;i++) { swap(num[i],num[k]); solve(ans,num,k+1); swap(num[i],num[k]); } }};
Permutations II:
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2]
have the following unique permutations:[1,1,2]
, [1,2,1]
, and [2,1,1]
.
其实如果可以用STL的函数next_permutation的话,一直next_permutation到false,每次把num加到结果里就行了,很简单吧~当然要先sort一下,不过面试时这样答的话,面试官就会让你实现next_permutation了~好在我们前面也做过啊~
另外一个方法就是用一个数组来记录一个数是否出现在一个位置过了
class Solution {public: vector<vector<int> > permuteUnique(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > ans; vector<int> cur; vector<int> used(num.size(),0); sort(num.begin(),num.end()); solve(ans,num,cur,used); return ans; } void solve(vector<vector<int> >& ans,vector<int>& num,vector<int>& cur,vector<int>& used) { int n=num.size(); if ( cur.size()==n) { ans.push_back(cur); return; } for(int i=0;i<n;i++) { if ( used[i]||(i!=0&&num[i-1]==num[i]&&used[i-1])) continue; cur.push_back(num[i]); used[i]=1; solve(ans,num,cur,used); used[i]=0; cur.pop_back(); } }};
另外就是用一个set记录num[i]是否已经放在K这个位置过了。
class Solution {public: vector<vector<int> > permuteUnique(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > ans; sort(num.begin(),num.end()); solve(ans,num,0); return ans; } void solve(vector<vector<int> >& ans,vector<int>& num,int k) { int n=num.size(); if ( k==n) { ans.push_back(num); return; } set<int> used; for(int i=k;i<n;i++) { if ( used.find(num[i])!=used.end()) continue; swap(num[i],num[k]); solve(ans,num,k+1); swap(num[i],num[k]); used.insert(num[i]); } }};
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.
class Solution {public:string getPermutation(int n, int k) {// Start typing your C/C++ solution below// DO NOT write int main() functionvector<int> canUse(n,0);vector<int> comp(n+1,1);for(int i=2;i<=n;i++)comp[i]=comp[i-1]*(i-1);string ans;k--;for(int i=n;i>0;i--){int t=k/comp[i];k=k%comp[i];ans.push_back(getUse(canUse,t)+'0');}return ans;}int getUse(vector<int>& canUse,int k){for(int i=0;i<canUse.size();i++){if(canUse[i]==0){if(k==0){canUse[i]=1;return i+1;}k--;}}return -1;}};
- [LeetCode] Next Permutation、Permutations、Permutations II、Permutation Sequence
- 【leetcode 排列组合问题】Next Permutation | Permutations | Permutations II | Permutation Sequence
- 【LeetCode】Permutations & Permutations II & Next Permutations & Permutation Sequence
- 46. Permutations&&47. Permutations II&31. Next Permutation
- 【C++】【LeetCode】31. Next Permutation && 46. Permutations
- N-Queens && II and Permutations && II && Next Permutation
- 46 permutations && 47 Permutation II
- Permutation, next Permutation, Permutation sequence
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- 60. Permutation Sequence&字符串排列&8.7Permutations without Dups
- Leetcode: Permutations and Permutations II
- leetcode-permutations and permutations II
- Leetcode:Permutations与Permutations II
- LintCode_190 Next Permutation II
- #190 Next Permutation II
- poj 2411 Mondriaan's Dream(状态压缩dp)
- Steps for creating android applications using NDK and ARM assembly language
- 对于0-1分数规划的Dinkelbach算法的分析
- CMurphi入门笔记(二)——基本概念
- cocos2d 中使用 pickerView 简单的老虎机应用
- [LeetCode] Next Permutation、Permutations、Permutations II、Permutation Sequence
- C++ 异常使用总结(持续更新)
- 数学之美--平凡而又神奇的贝叶斯方法(四)
- UVA 729 - The Hamming Distance Problem 解题报告
- SDJZU-FatMouse' Trade
- VC控件通知消息类型
- DWR一直处于加载状态的问题解决
- Baby-step giant-step
- 各大IT公司校园招聘程序猿笔试、面试题集锦