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]
]
题意就是说在一个set中寻找元素,使和为target,注意元素可以重复使用。典型的DFS深度优先搜索去做。
建议和这一道题leetcode 377. Combination Sum IV 组合之和 + DP动态规划 + DFS深度优先遍历、leetcode 216. Combination Sum III DFS + 按照index递归遍历 、leetcode 40. Combination Sum II DFS深度优先搜索 一起学习
注意这个是包含当前元素的
代码如下:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution
{
public List<List<Integer>> combinationSum(int[] candidates, int target)
{
List<List<Integer>> res=new ArrayList<List<Integer>>();
List<Integer> one=new ArrayList<>();
//考虑特殊情况
if(candidates==null || candidates.length<0)
return res;
else
{
Arrays.sort(candidates);
getSum(candidates,target,res,one,0,0);
return res;
}
}
private void getSum(int[] candidates, int target, List<List<Integer>> res,
List<Integer> one, int sum,int level)
{
if(sum>target)
return ;
else if(sum==target)
{
res.add(new ArrayList<>(one));
}else
{
//题目中说可以重复的使用某一个数,所以level可以不变
for(int i=level;i<candidates.length;i++)
{
one.add(candidates[i]);
//所以这里的level为i就是表示可以重复使用一个值
getSum(candidates, target, res, one, sum+candidates[i], i);
one.remove(one.size()-1);
}
}
}
}
下面的是C++的做法,就是一个经典的DFS深度优先搜索遍历的做法,很经典和典型,值得学习
其实上面的Java的递归做法逻辑上有点乱,一共两个临界情况:一个是Index一个是sum,那么直接分情况讨论即可
代码如下:
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>
using namespace std;
class Solution
{
public:
vector<vector<int>> res;
vector<vector<int>> combinationSum(vector<int>& can, int target)
{
sort(can.begin(), can.end());
dfs(can, target, 0, 0, vector<int>());
return res;
}
void dfs(vector<int>& can, int target, int sum, int index, vector<int> one)
{
if (index == can.size())
{
if (sum == target)
res.push_back(one);
return;
}
else
{
if (sum > target)
return;
else if (sum == target)
res.push_back(one);
else
{
for (int i = index; i < can.size(); i++)
{
one.push_back(can[i]);
dfs(can, target, sum + can[i], i, one);
one.pop_back();
}
}
}
}
};