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提供的工具快速并且节约内存.开发者可以使用这些工具创建属于自己特定的迭代器,这些特定的迭代器可以用于 ...
随机推荐
- SQL Server 两种判断表名是否存在且删除的方式
邓老师(老邓)教的 if exists(select * from sysobjects where name='Table_88') drop table Table_88 偷的((*^__^ ...
- 自定义view(自定义view的时候,三个构造函数各自的作用)
package com.timeshare.tmband.Utils; import android.content.Context; import android.content.res.Typed ...
- Android开发中一些常见的问题解决方案
分享一下自己开发中遇到的一些常见问题及解决方案,方面以后快速开发少走弯路,也可以供大家一起学习. 1.开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listvie ...
- typedef和define的作用域
typedef: 如果放在所有函数之外,它的作用域就是从它定义开始直到文件尾: 如果放在某个函数内,定义域就是从定义开始直到该函数结尾: #define: 不管是在某个函数内,还是在所有函数之外,作用 ...
- php中的短标签 太坑人了
今天配置了一个php页面去修改svn密码问题,结果调了半天,最后在Windows和 Linux的运行现象是不一样,运行结果更不一样了,关键是完全一模一样的代码. 最后发现是短标签引起的,Windows ...
- Linux命令记录。
引用:http://www.cnblogs.com/xiaoluo501395377/archive/2013/03/31/2992500.html 首先,需要确定的是知道的是:对于Linux系统来说 ...
- 理解Java的GC日志
分析如下GC日志:[GC [PSYoungGen: 9216K->1024K(9216K)] 1246196K->1246220K(1287040K), 0.2398360 secs] [ ...
- 关于meta定义 和 link
<!DOCTYPE html> <!-- HTML5 doctype 不区分大小写 --> <html lang="zh-cmn-Hans-CN"&g ...
- django之uWSGI配置 +Nginx
参考文档 官方文档 安装: pip install uwsgi 启动命令: 方法一.直接命令启动 /home/zabbix/application/python/bin/uwsgi --socke ...
- android的注意点
1.使用Message.callback Message msg = Message.obtain(myThreadHandler,new Runnable() { @Override public ...