Combination Sum |

Given a set of candidate numbers (C) 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.

For example, given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3]

Notice
  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.
 
Example

given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3]

分析:递归

 public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> listsAll = new ArrayList<>();
Arrays.sort(candidates);
helper(, , candidates, target, new ArrayList<>(), listsAll);
return listsAll;
} public static void helper(int index, int total, int[] candidates, int target, List<Integer> list, List<List<Integer>> listsAll) {
if (index >= candidates.length || total >= target) return;
list.add(candidates[index]);
total += candidates[index];
if (total == target) {
listsAll.add(new ArrayList<>(list));
}
helper(index, total, candidates, target, list, listsAll);
total = total - candidates[index];
list.remove(list.size() - );
helper(index + , total, candidates, target, list, listsAll);
}
}

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.

Notice
  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.
Example

Given candidate set [10,1,6,7,2,1,5] and target 8,

A solution set is:

[
[1,7],
[1,2,5],
[2,6],
[1,1,6]
]
 public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> listsAll = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
helper(, , candidates, target, new ArrayList<>(), listsAll);
return listsAll;
} public static void helper(int index, int total, int[] candidates, int target, List<Integer> list, List<List<Integer>> listsAll) {
if (index >= candidates.length || total >= target) return;
list.add(candidates[index]);
total += candidates[index];
if (total == target) {
listsAll.add(new ArrayList<Integer>(list));
}
helper(index + , total, candidates, target, list, listsAll);
total = total - candidates[index];
list.remove(list.size() - );
while (index + < candidates.length && candidates[index] == candidates[index + ]) {
index++;
}
helper(index + , total, candidates, target, list, listsAll);
}
}


Combination Sum IV

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

nums = [1, 2, 3]
target = 4 The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1) Note that different sequences are counted as different combinations. Therefore the output is 7.
分析: 这题和change coin非常相似。
 public class Solution {
public int combinationSum4(int[] nums, int target) {
if (nums == null || nums.length == ) return ; int[] dp = new int[target + ];
dp[] = ; for (int i = ; i <= target; i++) {
for (int num : nums) {
if (i - num >= ) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}
}
参考请注明出处:cnblogs.com/beiyeqingteng/ 

Combination Sum | & || & ||| & IV的更多相关文章

  1. LC 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  3. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  4. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  6. 377. Combination Sum IV

    问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...

  7. Leetcode 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  8. leetcode日记 Combination sum IV

    题目: Given an integer array with all positive numbers and no duplicates, find the number of possible ...

  9. Leetcode: Combination Sum IV && Summary: The Key to Solve DP

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. javamail技术

    package com.zh.javaEmail; import java.util.*; import javax.mail.*; import javax.mail.internet.*; imp ...

  2. .net架构设计读书笔记--第三章 第10节 命令职责分离(CQRS)简介(Introducing CQRS)

    一.分离查询命令 Separating commands from queries     早期的面向DDD设计方法的难点是如何设计一个类,这个类要包含域的方方面面.通常来说,任务软件系统方法调用可以 ...

  3. Java基础-final变量和普通变量的区别

    当用final作用于类的成员变量时,成员变量(注意是类的成员变量,局部变量只需要保证在使用之前被初始化赋值即可)必须在定义时或者构造器中进行初始化赋值,而且final变量一旦被初始化赋值之后,就不能再 ...

  4. java基础-关键字-native

     一. 什么是Native Method    简单地讲,一个Native Method就是一个java调用非java代码的接口.一个Native Method是这样一个java的方法:该方法的实现由 ...

  5. @SuppressWarnings含义

    J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. @SuppressWarnings 批注允许您选择 ...

  6. 【BZOJ-2588】Count on a tree 主席树 + 倍增

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 3749  Solved: 873[ ...

  7. 宿主机( win 7 系统) ping 虚拟机VMware( cent os 6.6 ) 出现“请求超时”或者“无法访问目标主机”的解决方法

    首先虚拟机的网络连接设置为"Host-only": 然后在 cmd 窗口中查看 VMnet1 的 ip 地址,这里是 192.168.254.1 接下来在 Linux 中设置网卡地 ...

  8. 循序渐进Linux 3:Linux下软件安装与管理

    一.源码安装 ./configuremakemake install 二.RPM包 1. 安装软件包 rpm -i [辅助选项] file1.rpm file2.rpm主选项 -i: install, ...

  9. MySQL逻辑备份与恢复

    备份:mysqldump -uroot -p yyzc department > /home/admin/yyzc_backup.sql 恢复:mysql -uroot -p yyzc < ...

  10. inux环境PHP7.0安装

    inux环境PHP7.0安装   PHP7和HHVM比较PHP7的在真实场景的性能确实已经和HHVM相当, 在一些场景甚至超过了HHVM.HHVM的运维复杂, 是多线程模型, 这就代表着如果一个线程导 ...