No.1
题目链接:Combination Sum
- 题目描述:
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7, A solution set is:
[ [7], [2, 2, 3] ]
(1)思路:使用深度优先的方法搜索,并且用一个vector记录向量,找到合适的向量时将它保存在结果中并进行回溯操作。
(2)代码:
class Solution {
public:
vector<vector<int> > combinationSum(vector<int>& candidates, int target) {
vector<vector<int> > result;
vector<int> path;
sort(candidates.begin(),candidates.end());
helper(candidates,0,0,target,path,result);
return result;
}
void helper(vector<int> &nums,int pos,int base,int target,vector<int>& path,vector<vector<int> > & result)
{
if(base==target)
{
result.push_back(path);
return ;
}
if(base>target)
return ;
for(int i=pos;i<nums.size();i++)
{
path.push_back(nums[i]);
helper(nums,i,base+nums[i],target,path,result);
path.pop_back();
}
}
};
(3)提交结果:
(4)测试代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
class Solution {
public:
vector<vector<int> > combinationSum(vector<int>& candidates, int target) {
vector<vector<int> > result;
vector<int> path;
sort(candidates.begin(),candidates.end());
helper(candidates,0,0,target,path,result);
return result;
}
void helper(vector<int> &nums,int pos,int base,int target,vector<int>& path,vector<vector<int> > & result)
{
if(base==target)
{
result.push_back(path);
return ;
}
if(base>target)
return ;
for(int i=pos;i<nums.size();i++)
{
path.push_back(nums[i]);
helper(nums,i,base+nums[i],target,path,result);
path.pop_back();
}
}
};
int main(){
int n;
cout<<"请输入向量长度:";
cin>>n;
vector<int> nums;
int a;
for(int i=0;i<n;i++){
cin>>a;
nums.push_back(a);
}
int target;
cout<<"请输入target:";
cin>>target;
Solution solution;
vector<vector<int> > result=solution.combinationSum(nums, target);
int size = result.size();
for(int i=0;i<size;i++){
int size1=result[i].size();
cout<<"[";
int j;
for(j=0;j<size1-1;j++){
cout<<result[i][j]<<", ";
}
cout<<result[i][j]<<"]"<<endl;
}
}
No.2
题目链接:Combination Sum II
- 题目描述:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8, A solution set is:
[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]
(1)思路:递归回溯,但注意在同一层递归树中,如果某元素已经处理并进入下一层递归,那么与该元素相同的值就应该跳过,否则将出现重复;例如:1,1,2,3 如果第一个1已经处理并进入下一层递归1,2,3。那么第二个1就应该跳过,因为后续所有情况都已经被覆盖掉。 另外,相同元素第一个进入下一层递归,而不是任意一个。例如:1,1,2,3。如果第一个1已经处理并进入下一层递归1,2,3,那么两个1是可以同时成为可行解的,而如果选择的是第二个1并进入下一层递归2,3,那么不会出现两个1的解了。
(2)代码:
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int> > ret;
vector<int> cur;
Helper(ret, cur, candidates, target, 0);
return ret;
}
void Helper(vector<vector<int> > &ret, vector<int> cur, vector<int> &candidates, int target, int position){
if(target == 0)
ret.push_back(cur);
else{
for(int i = position; i < candidates.size() &&candidates[i] <= target; i ++){
if(i != position && candidates[i] == candidates[i-1])
continue;
cur.push_back(candidates[i]);
Helper(ret, cur, candidates, target-candidates[i], i+1);
cur.pop_back();
}
}
}
};
(3)提交结果:
(4)测试代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int> > ret;
vector<int> cur;
Helper(ret, cur, candidates, target, 0);
return ret;
}
void Helper(vector<vector<int> > &ret, vector<int> cur, vector<int> &candidates, int target, int position){
if(target == 0)
ret.push_back(cur);
else{
for(int i = position; i < candidates.size() &&candidates[i] <= target; i ++){
if(i != position && candidates[i] == candidates[i-1])
continue;
cur.push_back(candidates[i]);
Helper(ret, cur, candidates, target-candidates[i], i+1);
cur.pop_back();
}
}
}
};
int main(){
int n;
cout<<"请输入向量长度:";
cin>>n;
vector<int> nums;
int a;
for(int i=0;i<n;i++){
cin>>a;
nums.push_back(a);
}
int target;
cout<<"请输入target:";
cin>>target;
Solution solution;
vector<vector<int> > result=solution.combinationSum2(nums, target);
int size = result.size();
for(int i=0;i<size;i++){
int size1=result[i].size();
cout<<"[";
int j;
for(j=0;j<size1-1;j++){
cout<<result[i][j]<<", ";
}
cout<<result[i][j]<<"]"<<endl;
}
}
No.3
题目链接:Unique Paths
- 题目描述:
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
(1)思路:动态规划,对于点(i, j),只能从上边的点(i-1,j)或者左边的点(i,j-1)到达,且两者路径是不重复的,因此path[i][j] = path[i-1][j]+path[i][j-1]。
(2)代码:
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > path(m, vector<int>(n, 1));
for(int i = 1; i < m; i ++) {
for(int j = 1; j < n; j ++) {
path[i][j] = path[i-1][j] + path[i][j-1];
}
}
return path[m-1][n-1];
}
};
(3)提交结果:
(4)测试代码:
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > path(m, vector<int>(n, 1));
for(int i = 1; i < m; i ++)
{
for(int j = 1; j < n; j ++)
{
path[i][j] = path[i-1][j] + path[i][j-1];
}
}
return path[m-1][n-1];
}
};
int main(){
int m, n;
cout<<"请输入m n :";
cin>>m>>n;
Solution solution;
int result = solution.uniquePaths(m, n);
cout<<result<<endl;
return 0;
}
No.4
题目链接:Permutations
- 题目描述:
Given a collection of distinct 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], [3,2,1] ]
(1)思路:我们可以采用递归的方式。设置一个中间位置变量pos,让它初始化为0。用一层循环让该位置的数与其他所有位置的数交换位置,在该循环中进行递归调用,调用时将pos设为pos+1。直至pos的值等于整数向量的size-1即表示完成。
(2)测试代码:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
vector<vector<int> > permute(vector<int>& nums) {
vector<vector<int> > ret;
Helper(ret, nums, 0);
return ret;
}
void Helper(vector<vector<int> >& ret, vector<int> num, int pos)
{
if(pos == num.size()-1)
ret.push_back(num);
else
{
for(int i = pos; i < num.size(); i ++)
{
swap(num[pos], num[i]);
Helper(ret, num, pos+1);
swap(num[pos], num[i]);
}
}
}
};
int main(){
int n;
cout<<"请输入向量长度:";
cin>>n;
vector<int> nums;
int a;
for(int i=0;i<n;i++){
cin>>a;
nums.push_back(a);
}
Solution solution;
vector<vector<int> > result=solution.permute(nums);
int size = result.size();
for(int i=0;i<size;i++){
int size1=result[i].size();
cout<<"[";
int j;
for(j=0;j<size1-1;j++){
cout<<result[i][j]<<", ";
}
cout<<result[i][j]<<"]"<<endl;
}
}