leetcode_permutaionu全排列集合_31_46_47_60
2015-04-21 14:56
260 查看
这是leetcode上一个很有意思的排列集合
在此之前我想先引用leetcode上discuss里一位仁兄提出的问题:
其中最高票的回答是:
Definitely raise this concern to your interviewer before answering the question. It is good only when your interviewer says
so. However, it is verly likely that he or she will ask you to implement next_permutation all by yourself, so be prepared.
我觉得这也应该是我们对于重复造轮子的理解,无论是简单的为了应付面试还是加深自己的思维深度。
因此如果你的目标不仅仅是在题目列表前多加一个√:
It will make you more like a computer scientist,cheers!
所以在这里我会一次提供STL的解法和使用递归构造的解法。
传送门:
31:https://leetcode.com/problems/next-permutation/
AC代码1:
46:https://leetcode.com/problems/permutations/
AC代码1:
具体思路是使得所有可能的集合都会出现。
47:
传送门:https://leetcode.com/problems/permutations-ii/
具体思路是阻断所有重复的出现 / 允许出现但不加入最终的答案中。
AC代码1:
这道题所花费的时间在cpp里是最多的。
60:
在此之前我想先引用leetcode上discuss里一位仁兄提出的问题:
use the function "STL next_permutation" to solve this problem. good or BAD???
其中最高票的回答是:Definitely raise this concern to your interviewer before answering the question. It is good only when your interviewer says
so. However, it is verly likely that he or she will ask you to implement next_permutation all by yourself, so be prepared.
我觉得这也应该是我们对于重复造轮子的理解,无论是简单的为了应付面试还是加深自己的思维深度。
因此如果你的目标不仅仅是在题目列表前多加一个√:
It will make you more like a computer scientist,cheers!
所以在这里我会一次提供STL的解法和使用递归构造的解法。
传送门:
31:https://leetcode.com/problems/next-permutation/
AC代码1:
class Solution { public: void nextPermutation(vector<int> &num) { next_permutation(num.begin(),num.end()); } };
46:https://leetcode.com/problems/permutations/
AC代码1:
具体思路是使得所有可能的集合都会出现。
class Solution { public: vector<vector<int> > permute(vector<int> &num) { vector<vector<int> > ans; sort(num.begin(),num.end()); do{ ans.push_back(num); }while(next_permutation(num.begin(),num.end())); return ans; } };
47:
传送门:https://leetcode.com/problems/permutations-ii/
具体思路是阻断所有重复的出现 / 允许出现但不加入最终的答案中。
AC代码1:
这道题所花费的时间在cpp里是最多的。
class Solution { public: bool check(vector<int> &num,vector<vector<int> > &ans) { for(int i=0;i<ans.size();i++) if(num==ans[i]) return false; return true; } vector<vector<int> > permuteUnique(vector<int> &num) { vector<vector<int> > ans; sort(num.begin(),num.end()); do{ if(check(num,ans)) ans.push_back(num); }while(next_permutation(num.begin(),num.end())); return ans; } };
60:
相关文章推荐
- leetcode46/47-Permutations I/II(全排列问题)
- LeetCode46,47 Permutations, Permutations II
- LeetCode39/40/22/77/17/78/51/46/47/79 10道 Backtracking**
- [Leetcode] 46, 47, 77
- LeetCode-46 Permutations(全排列)
- LeetCode 46 Permutations + LeetCode 47 Permutations II
- LeetCode-----46.Permutations&&47.Permutations II (全排列----回溯法)
- 深度优先-Leetcode46 全排列
- 【LeetCode】31. Next Permutations 下一个全排列
- leetcode-46、47 Permutations/II 数字的排列组合
- leetcode-47. Permutations II(重复元素全排列)
- 2018.1.13 LeetCode 31. Next Permutation 46. Permutations【STL -> 全排列】
- [leetcode 46] Permutations------数组中元素的所有排列组合集合
- LeetCode 2016.3.18 47,31
- LeetCode 46/47. Permutations i, ii
- leetcode之深搜递归回溯类之排列与组合类-----77/39/40/216/317 组合 78/90/368 子排列 22/79/93/131 典型递归回溯 46/47 全排列
- [LeetCode 46 & 47] Permutations I & II
- LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)
- 【LeetCode51-60】N皇后,和最大子串,螺旋矩阵,跳跳棋,合并区间,第K个全排列
- LeetCode 46 Permutations(全排列问题)