前三道组合问题都是很简单的用递归就能解决的
但是第四道用回溯就超时了,当然可以用记忆化搜索的方式,但是我看了题解后,决定记录一下最简单的动态规划的思路

class Solution {
private List<List<Integer>> res = new LinkedList<>();
private void dfs(int n, int k, LinkedList<Integer> ls, boolean sign[], int index){
if(ls.size() > k){
return;
}
if(ls.size() == k){
res.add(new LinkedList<>(ls));
return;
}
for(int i=index; i<=n; i++){
if(sign[i]){
continue;
}
ls.addLast(i);
sign[i] = true;
dfs(n, k, ls, sign, i);
sign[i] = false;
ls.removeLast();
}
}
public List<List<Integer>> combine(int n, int k) {
List<Integer> ls = new LinkedList<>();
boolean sign[] = new boolean[n+1];
dfs(n, k, (LinkedList<Integer>) ls, sign, 1);
return res;
}
}


class Solution {
List<List<Integer>> res = new LinkedList<>();
private void dfs(int[] candidates, int target, int sum, LinkedList<Integer> ls, int index){
if(sum > target){
return;
}
if(sum == target){
res.add(new LinkedList<>(ls));
return;
}
for(int i=index; i<candidates.length; i++){
// 剪枝
if(candidates[i] > target){
break;
}
ls.addLast(candidates[i]);
dfs(candidates, target, sum + candidates[i], ls, i);
ls.removeLast();
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
LinkedList<Integer> ls = new LinkedList<>();
Arrays.sort(candidates);
dfs(candidates, target, 0, ls, 0);
return res;
}
}


class Solution {
List<List<Integer>> res;
Set<List<Integer>> set = new HashSet<>();
private void dfs(int[] candidates, int target, boolean sign[], int sum, LinkedList<Integer> ls, int index){
if(sum > target){
return;
}
if(sum == target){
set.add(new LinkedList<>(ls));
return;
}
for(int i=index; i<candidates.length; i++){
if(candidates[i] > target){
break;
}
if(sign[i]){
continue;
}
sign[i] = true;
sum += candidates[i];
ls.addLast(candidates[i]);
dfs(candidates, target, sign, sum, ls, i);
ls.removeLast();
sum -= candidates[i];
sign[i] = false;
}
}
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
if(candidates.length == 0){
return res;
}
Arrays.sort(candidates);
LinkedList<Integer> ls = new LinkedList<>();
boolean sign[] = new boolean[candidates.length];
dfs(candidates, target, sign, 0, ls, 0);
res = new LinkedList<>();
for(List<Integer> list: set){
res.add(list);
}
return res;
}
}



下面这题如果不用记忆化搜索的画,就超时了......

dp[i] :对于给定的由正整数组成且不存在重复数字的数组,和为 i 的组合的个数。
nums=[1, 3, 4], target=7;
* dp[7] = dp[6] + dp[4] + dp[3]
* 即:7 的组合数可以由三部分组成,1 和 dp[6],3 和 dp[4], 4 和dp[3];
public class Solution {
/**
* 这里状态定义就是题目要求的,并不难,状态转移方程要动点脑子,也不难:
* 状态转移方程:dp[i]= dp[i - nums[0]] + dp[i - nums[1]] + dp[i - nums[2]] + ... (当 [] 里面的数 >= 0)
* 特别注意:dp[0] = 1,表示,如果那个硬币的面值刚刚好等于需要凑出的价值,这个就成为 1 种组合方案
* 再举一个具体的例子:nums=[1, 3, 4], target=7;
* dp[7] = dp[6] + dp[4] + dp[3]
* 即:7 的组合数可以由三部分组成,1 和 dp[6],3 和 dp[4], 4 和dp[3];
*
* @param nums
* @param target
* @return
*/
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target + 1];
// 这个值被其它状态参考,设置为 1 是合理的
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (int num : nums) {
if (num <= i) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}
}


被折叠的 条评论
为什么被折叠?



