不能重复:

[抄题]:

给出一个候选数字的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. (转!)大话websocket

    邪正看眼鼻,真假看嘴唇,功名看气概,富贵看精神. ---曾国藩<冰鉴> 转自https://www.cnblogs.com/fuqiang88/p/5956363.html 原文http: ...

  2. UMG设置组件自适应居中或靠边

    转自:http://aigo.iteye.com/blog/2297430 比如你的UI中有些组件需要居中显示,有些需要始终在右上角并且与上边框和右边框保持固定的距离等等,对于这种需要动态适应屏幕分辨 ...

  3. javascript继承之原型链(一)

    function Father() { this.fatherValue = "爸爸"; } Father.prototype.getFatherValue = function ...

  4. python并发编程之多进程理论部分

    原文连接:http://www.cnblogs.com/linhaifeng/articles/7430066.html#_label4 一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责 ...

  5. tomcat启动原理

    2018年04月12日 19:55:22 太极小帅帅 阅读数:282   前言 一直在用Tomcat,但是对其启动原理一直没去研究,这里准备去面试,可能会问道.于是总结了下启动原理.完全凭感觉去揣测, ...

  6. 《汇编语言 基于x86处理器》前五章的小程序

    ▶ 书中前五章的几个小程序,基本的运算操作,使用了作者的库 Irvine32 和 Irvine64(一开始以为作者网站过期了,各网站上找到的文件大小都不一样,最后发现是要搭梯子 Orz,顺利下载).注 ...

  7. 使用three.js开发3d地图初探

    three是图形引擎,而web二维三维地图都是基于图形引擎的,所以拿three来开发需求简单的三维地图应用是没什么问题的. 1.坐标转换 实际地理坐标为经度.纬度.高度,而three.js使用的是右手 ...

  8. Alpha版本检测报告

    1.Alpha版本测试报告 发布一篇随笔,作为项目的测试报告,内容包括: (1)测试计划 测试人员 工作安排 覃一霸 编写测试报告 张江波 执行测试.截图测试结果 测试功能 功能 描述 效果 结果 登 ...

  9. linux查看磁盘占用常用的两个命令

    1.查看总容量.已使用.未使用容量:df -hl -h:以kb以上单位显示 -l:仅显示本地文件系统 2.查看当前路径下,每个文件/夹占用空间大小:du -sh *

  10. 前端-CSS-9-文本和字体-背景颜色

    字体属性 div{ width: 300px; height: 100px; /*background-color: red;*/ border: 1px solid red; /*设置字体大小 px ...