Permutations,Permutations II,Combinations
这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型
解题步骤:
1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了。
2:是否允许重复组合
3:处理某个数,判断结果
4:dfs递归
5:还原现场
一: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 {
void dfs(vector<int>& nums,int start,vector<vector<int>>& res,vector<int>& oneRes){
int n = nums.size();
if(start == n){
res.push_back(oneRes);
}
for(int i= start;i<nums.size();++i){
if(i>start && nums[i]==nums[i-]){
continue;
}
oneRes.push_back(nums[i]);
swap(nums[i],nums[start]);
dfs(nums,start+,res,oneRes);
swap(nums[i],nums[start]);
oneRes.pop_back();
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
vector<int> oneRes;
dfs(nums,,res,oneRes);
return res;
}
};
二: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].
方法1.
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
vector<int> tmp(nums);
sort(tmp.begin(),tmp.end());
res.push_back(tmp);
while(next_permutation(tmp.begin(),tmp.end())){
res.push_back(tmp);
}
return res;
}
};
方法2.
class Solution {
void dfs(vector<int>& nums,int start,vector<vector<int>>& res,vector<int>& oneRes){
int n = nums.size();
if(start == n){
res.push_back(oneRes);
}
for(int i= start;i<nums.size();++i){
if(i>start && nums[i]==nums[i-]){
continue;
}
int selectNum = nums[i];
oneRes.push_back(selectNum);
copy_backward(nums.begin()+start,nums.begin()+i,nums.begin()+i+);
nums[start] = selectNum;
dfs(nums,start+,res,oneRes);
copy(nums.begin()+start+,nums.begin()+i+,nums.begin()+start);
nums[i] = selectNum;
oneRes.pop_back();
}
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
vector<int> oneRes;
sort(nums.begin(),nums.end());
dfs(nums,,res,oneRes);
return res;
}
};
方法3.
class Solution {
public:
void dfs(vector<int> nums,int numsSize,int startPos,vector<vector<int>>& res,vector<int>& oneOfRes)
{
sort(nums.begin()+startPos,nums.end());
for(int i=startPos;i<numsSize;i++){
if(i>startPos && nums[i]==nums[i-]){
continue;
}
oneOfRes.push_back(nums[i]);
swap(nums[i],nums[startPos]);
if(oneOfRes.size()==numsSize){
res.push_back(oneOfRes);
}else{
dfs(nums,numsSize,startPos+,res,oneOfRes);
}
swap(nums[i],nums[startPos]);
oneOfRes.pop_back();
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
// sort(nums.begin(),nums.end());
vector<vector<int>> res;
vector<int> oneOfRes;
int numsSize = nums.size();
dfs(nums,numsSize,,res,oneOfRes);
return res;
}
};
77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
class Solution {
void dfs(int n,int start,int k,int curk,vector<vector<int>>& res,vector<int>& oneRes){
if(curk == ){
res.push_back(oneRes);
return;
}
for(int i=start;i<=n;++i){
oneRes.push_back(i);
dfs(n,i+,k,curk-,res,oneRes);
oneRes.pop_back();
}
}
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> res;
vector<int> oneRes;
dfs(n,,k,k,res,oneRes);
return res;
}
};
Permutations,Permutations II,Combinations的更多相关文章
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- LeetCode46,47 Permutations, Permutations II
题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) ...
- LeetCode解题报告—— Permutations & Permutations II & Rotate Image
1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...
- Permutations I&&II
Permutations I Given a collection of distinct numbers, return all possible permutations. For example ...
- Combination Sum II Combinations
https://leetcode.com/problems/combination-sum-ii/ 题目跟前面几道题很类似,直接写代码: class Solution { public: vector ...
- leetcode difficulty and frequency distribution chart
Here is a difficulty and frequency distribution chart for each problem (which I got from the Interne ...
- Leetcode——回溯法常考算法整理
Leetcode--回溯法常考算法整理 Preface Leetcode--回溯法常考算法整理 Definition Why & When to Use Backtrakcing How to ...
- Python标准模块--itertools
1 模块简介 Python提供了itertools模块,可以创建属于自己的迭代器.itertools提供的工具快速并且节约内存.开发者可以使用这些工具创建属于自己特定的迭代器,这些特定的迭代器可以用于 ...
随机推荐
- (转)C#创建datatable
Asp.net DataTable添加列和行的方法 方法一: DataTable tblDatas = new DataTable("Datas"); DataColumn dc ...
- V - stl 的 优先队列 Ⅱ
Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morni ...
- c++ 资源索引
1.http://snippets.dzone.com/tag/c/ --数以千计的有用的C语言源代码片段 2.http://www.hotscripts.com/category/c-cpp/sc ...
- delete、update忘加where条件误操作恢复过程演示
update.delete没有带where条件,误操作,如何恢复呢? 我现在有一张学生表,我要把小于60更新成不及格. mysql> select * from student; +----+- ...
- png压缩工具-PngoutWin
PngoutWin是一款聪明的png图片压缩工具,别的压缩工具压缩PNG是通过丢弃透明层来达到减肥的目的.可是不能透明的PNG还能叫PNG吗?PngoutWin它不会丢弃原本的透明,而是通 ...
- python连接redis文档001
Installation redis-py requires a running Redis server. See Redis’s quickstart for installation instr ...
- Redis 数据库的安装
安装redis真的是简单到不行了,三条命令就可以完成. 1.下载redis: 001.http://redis.io/ 002.也可以在我的网盘下载https://yunpan.cn/crSUX6aS ...
- 正式学习React(一) 开始学习之前必读
为什么要加这个必读!因为webpack本身是基于node环境的, 里面会涉及很多路径问题,我们可能对paths怎么写!webpack又是怎么找到这些paths的很迷惑. 本文是我已经写完正式学习Rea ...
- 以Qemu模拟Linux,学习Linux内核
文章名称:以Qemu模拟Linux,学习Linux内核作 者:five_cent文章地址:http://www.cnblogs.com/senix/archive/2013/02/21/29 ...
- hdu 4686 Arc of Dream_矩阵快速幂
题意:略 构造出矩阵就行了 | AX 0 AXBY AXBY 0 | ...