LeetCode:40. Combination Sum II(Medium)
1. 原题链接
https://leetcode.com/problems/combination-sum-ii/description/
2. 题目要求
给定一个整型数组candidates[ ]和目标值target,找出数组中累加之后等于target的所有元素组合
注意:(1)每个可能的答案中,数组中的每一个元素只能使用一次;(2)数组存在重复元素;(3)数组中都是正整数;(4)不能存在重复解
3. 解题思路
这与第39题 Combination Sum 看起来很是类似,但一些细节要求完全不同,因此处理起来的方法也不相同。相同的是依旧采用递归的方法。
不存在重复解,但给定的数组中存在重复元素,因此先对candidates[ ]进行排序,方便处理重复元素的问题
4. 代码实现
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class CombinationSum40 {
public static void main(String[] args) {
CombinationSum40 cs = new CombinationSum40();
int[] candidates = {10, 1, 2, 7, 6, 1, 5};
for (List l : cs.combinationSum2(candidates, 8))
System.out.println(l);
} public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
for(int x:candidates)
System.out.print(x+" ");
System.out.println();
List<List<Integer>> result = new ArrayList<List<Integer>>();
combinationSum(result, new ArrayList<Integer>(), candidates, 0, target);
return result; } public void combinationSum(List<List<Integer>> result, List<Integer> tmp, int[] candidates, int start, int target) {
if (target > 0) {
for (int i = start; i < candidates.length; i++) {
if (i > start && candidates[i] == candidates[i - 1]) // 避免重复的结果
continue; // 结束for循环中其后的语句,跳回for循环
tmp.add(candidates[i]);
combinationSum(result, tmp, candidates, i + 1, target - candidates[i]);
tmp.remove(tmp.size() - 1); // 累加和超过target,则删去列表表尾元素
}
}
if (target == 0) {
result.add(new ArrayList<>(tmp));
} }
}
LeetCode:40. Combination Sum II(Medium)的更多相关文章
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- LeetCode OJ 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- LeetCode:12. Integer to Roman(Medium)
1. 原题链接 https://leetcode.com/problems/integer-to-roman/description/ 2. 题目要求 (1) 将整数转换成罗马数字: (2) 整数的范 ...
- LeetCode 40. Combination Sum II (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
- [LeetCode] Combination Sum II (递归)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- SVG基本图形
SVG 是使用 XML 来描述二维图形和绘图程序的语言 SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用来定义用于网络的基于矢量的图形 SVG 使用 XML ...
- java:transient是什么,有什么作用
参考博客:Java transient关键字 “Java的serialization提供了一种持久化对象实例的机制.当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机 ...
- 输出预测边界框,NMS非极大值抑制
我们预测阶段时: 生成多个锚框 每个锚框预测类别和偏移量 但是,当同一个目标上可能输出较多的相似的预测边界框.我们可以移除相似的预测边界框.——NMS(非极大值抑制). 对于一个预测边界框B,模型会计 ...
- 【HAOI2010】工厂选址题解
题目描述 某地区有m座煤矿,其中第i号矿每年产量为ai吨,现有火力发电厂一个,每年需用煤b吨,每年运行的固定费用(包括折旧费,不包括煤的运费)为h元,每吨原煤从第i号矿运到原有发电厂的运费为Ci0(i ...
- 理解JavaScript对象
理解JavaScript对象 对象是JavaScript的基本数据类型.对象是一种复合值:将很多值(原始值或者其他对象)聚合在一起. JavaScript对象不仅可以保持自有的属性,还可以从原型对象继 ...
- 二十三、详述 IntelliJ IDEA 中恢复代码的方法「进阶篇」
咱们已经了解了如何将代码恢复至某一版本,但是通过Local History恢复代码有的时候并不方便,例如咱们将项目中的代码进行了多处修改,这时通过Local History恢复代码就显得很麻烦,因为它 ...
- JVM 内部原理
1.JVM的组成: JVM 由类加载器子系统.运行时数据区.执行引擎以及本地方法接口组成. 2.JVM的运行原理: JVM是java的核心和基础,在java编译器和os平台之间的虚拟处理器.它是一种基 ...
- Android 发版的小工具
Android加固包签名 我们知道自己的apk在上传市场的时候, 为了更好的包含我们的代码需要加固服务, 加固后的apk是不能直接安装的, 需要我们手动签名. 关于Android签名的知识就不在赘述了 ...
- oracle之DQL
一.单表查询 语法:select * from table where 条件 group by 分组 having 过滤分组 order by 排序 --查询平均工资低于2000的部门的最大工资和平均 ...
- ABAP术语-BAPI ExplorerSupertype
Supertype 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/17/1109837.html Object type from whic ...