不能重复:

[抄题]:

给出一个候选数字的set(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T。C中的数字可以无限制重复被选取。

例如,给出候选数组[2,3,6,7]和目标数字7,所求的解为:

[7],

[2,2,3]

[思维问题]:

  1. 以为要在dfs函数中不断添加,其实用的是two sum的思想:反向寻找sum - nums[i]
  2. 为了避免重复取数,需要先排序去重

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. index和前面数不同时才+1,因此新数组容量需要+1
  2. 如果remainTarget比nums[i]小,直接break,退出所有循环

[二刷]:

  1. 数组要先排序,再去重
  2. DFS中应该先是返回条件,再是循环中的循环退出条件。循环中的参数是i,不是startIndex,startIndex是所有数组的开头
  3. combinations.add(nums[i]);添加的是数组中的元素,不是角标,毕竟是对元素进行处理

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

helper函数反复选i

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

public class Solution {
/**
* @param candidates: A list of integers
* @param target:An integer
* @return: A list of lists of integers
*/
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> results = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return results;
} int[] nums = removeDuplicates(candidates); dfs(nums, 0, new ArrayList<Integer>(), target, results); return results;
} private int[] removeDuplicates(int[] candidates) {
Arrays.sort(candidates); int index = 0;
for (int i = 0; i < candidates.length; i++) {
if (candidates[i] != candidates[index]) {
candidates[++index] = candidates[i];
}
} int[] nums = new int[index + 1];
for (int i = 0; i < index + 1; i++) {
nums[i] = candidates[i];
} return nums;
} private void dfs(int[] nums,
int startIndex,
List<Integer> combination,
int remainTarget,
List<List<Integer>> results) {
if (remainTarget == 0) {
results.add(new ArrayList<Integer>(combination));
return;
} for (int i = startIndex; i < nums.length; i++) {
if (remainTarget < nums[i]) {
break;
}
combination.add(nums[i]);
dfs(nums, i, combination, remainTarget - nums[i], results);
combination.remove(combination.size() - 1);
}
}
}

能重复:

[抄题]:

[思维问题]:

知道:不用remove duplicate函数。先排序,helper函数中改成i+1

结果:

[7,1,2,5,1,6,10]
8
输出[[1,1,6],[1,2,5],[1,7],[1,2,5],[1,7],[2,6]]
期望[[1,1,6],[1,2,5],[1,7],[2,6]]

问题:不知道怎么结果去重

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

public class Solution {
/**
* @param num: Given the candidate numbers
* @param target: Given the target number
* @return: All the combinations that sum to target
*/
public List<List<Integer>> combinationSum2(int[] candidates,
int target) {
List<List<Integer>> results = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return results;
} Arrays.sort(candidates);
List<Integer> combination = new ArrayList<Integer>();
helper(candidates, 0, combination, target, results); return results;
} private void helper(int[] candidates,
int startIndex,
List<Integer> combination,
int target,
List<List<Integer>> results) {
if (target == 0) {
results.add(new ArrayList<Integer>(combination));
return;
} for (int i = startIndex; i < candidates.length; i++) {
if (i != startIndex && candidates[i] == candidates[i - 1]) {
continue;
}
if (target < candidates[i]) {
break;
}
combination.add(candidates[i]);
helper(candidates, i + 1, combination, target - candidates[i], results);
combination.remove(combination.size() - 1);
}
}
}

数字组合 · Combination Sum的更多相关文章

  1. 【LeetCode】 数相加组合 Combination Sum

    描述 Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), ...

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

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

  3. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  4. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

  5. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  6. Leetcode之回溯法专题-39. 组合总数(Combination Sum)

    Leetcode之回溯法专题-39. 组合总数(Combination Sum) 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  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] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  9. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

随机推荐

  1. Python 示例 饮水记录

    因为每天都需要喝水  这是非常重要的 目录结构: ├─bin│ │ start.py│ ││ └─__pycache__│ start.cpython-36.pyc│├─core│ │ src.py│ ...

  2. [UE4]崩溃的原因收录

    UTool tool; 这样声明可以编译通过,但是UE4 Editor会直接崩溃. 应该改成这样: UTool* tool;

  3. Python 基础 json 与pickle

    json 支持:    str,int,tuple,list,dictpickle    支持python里所有的数据类型(包括函数)    只能在python中使用 json 与pickle 是一种 ...

  4. 使用for...of 优点,代替for...in,forEach和for循环

    来自阮一峰ES6标准: http://es6.ruanyifeng.com/#docs/iterator

  5. 编码风格和PEP8规范

    编码风格 错误认知 这很浪费时间 我是个艺术家 所有人都能穿的鞋不会合任何人的脚 我善长制定编码规范 正确认知 促进团队合作 减少bug处理 提高可读性,降低维护成本 有助于代码审查 养成习惯,有助于 ...

  6. ASCII和万国码

    ASCII和万国码 什么是ASCII 计算机的起初是使用内存中的0101来表示数和机器码.如何用内存中的bit来表示文本一直困扰着人们,毕竟人类主要的信息展示是文字,而不是苦涩的0101.后来ASCI ...

  7. redhat 7安装CentOS 7 yum源

    http://www.bubuko.com/infodetail-2004218.html http://www.bubuko.com/infodetail-2004218.html ******** ...

  8. 51. linux卸载jdk

    看有的资料上说有些Linux自带的有jdk1.4:如果自带的有jdk1.4的话首先要把jdk1.4卸载掉.具体做法: 输入命令: #rpm -qa | grep gcj 如果输出没有内容,说明没有jd ...

  9. FindWindow 查找窗口

    Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As ...

  10. fb 发布桌面应用图标

    1.以src文件夹为根目录,即图标放在src文件内 2.修改app.xml文件icon,按尺寸填入,如下图 同时可修改app应用的名字,接受中文,如下图红色涂鸦: 导出发行版的时候,注意打包内容有么有 ...