数字组合 · Combination Sum
不能重复:
[抄题]:
给出一个候选数字的set(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T。C中的数字可以无限制重复被选取。
例如,给出候选数组[2,3,6,7]和目标数字7,所求的解为:
[7],
[2,2,3]
[思维问题]:
- 以为要在dfs函数中不断添加,其实用的是two sum的思想:反向寻找sum - nums[i]
- 为了避免重复取数,需要先排序去重
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- index和前面数不同时才+1,因此新数组容量需要+1
- 如果remainTarget比nums[i]小,直接break,退出所有循环
[二刷]:
- 数组要先排序,再去重
- DFS中应该先是返回条件,再是循环中的循环退出条件。循环中的参数是i,不是startIndex,startIndex是所有数组的开头
- 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的更多相关文章
- 【LeetCode】 数相加组合 Combination Sum
描述 Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- Leetcode之回溯法专题-39. 组合总数(Combination Sum)
Leetcode之回溯法专题-39. 组合总数(Combination Sum) 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [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 ...
随机推荐
- 样本稳定指数PSI
信用评定等级划分之后需要对评级的划分做出评价,分析这样的评级划分结果是否具有实用价值,即分析样本分布的稳定程度.样本分布稳定,则信用评定等级划分结果的实用价值就高.采用样本稳定指数( PSI )检验样 ...
- iOS TTF文件改变字体
TTF(True Type Font):是一种字库名称 TTF文件:是Apple公司和Microsoft公司共同推出的字体文件格式 使用: 1 获取字体文件 从各种渠道下载字体ttf,网站或从别的ip ...
- 从线性回归到CNN【转】
原地址:http://zhangliliang.com/2014/06/14/from-lr-to-cnn/ csdn: http://blog.csdn.net/t0903/article/d ...
- Ubuntu 提权漏洞(CVE-2019-7304)复现
漏洞描述: Ubuntu 版本: Ubuntu 18.10 Ubuntu 18.04 LTS Ubuntu 16.04 LTS Ubuntu 14.04 LTS 2.28 < snapd < ...
- Python 之 cas-clinet
因为要搞一个用户登录安全的验证,要用到cas服务,所以在网上搜了很多关于cas信息才搞成功. 我写的属于客户端的cas就是从CAS服务,获取返回的ticket验证通过,用户登录成功. 使用的是web. ...
- JQ-用户注册用到的图形验证码,短信验证码点击事件,切换active类
// 点击切换图形验证码 页面加载完后执行,类似window.onload $(function () { var imgCaptcha = $(".img-captcha"); ...
- html-字体
字体大小 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- (11/24) css进阶:Less文件的打包和分离
写在前面:在前面我们对css打包和分离进行了描述.此节我们开始学习如何对less文件进行打包和分离. Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性, ...
- OpenCL 管道
▶ 按书上写的管道的代码,需要使用 OpenCL2.0 的平台和设备,目前编译不通过,暂时不知道是什么问题,先把代码堆上来,以后换了新的设备再说 ● 程序主要功能:用主机上的数组 srcHost 创建 ...
- leetcode743
class Solution { public: int networkDelayTime(vector<vector<int>>& times, int N, int ...