40. 组合总和 II leetcode JAVA
题目:
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
说明:
- 所有数字(包括目标数)都是正整数。
- 解集不能包含重复的组合。
示例 1:
输入: candidates =[10,1,2,7,6,1,5], target =8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]
解题思路:
首先将数组排序,然后递归地找到符合target的数组组合,最后除去重复的数组。
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(candidates);
getAnswers(res,candidates,target,new ArrayList<>(),0);
return res;
}
public void getAnswers(List<List<Integer>> res, int[] candidates, int target,
List<Integer> tempList,int index) {
if (target == 0) {
if(!res.contains(tempList))
res.add(tempList);
return;
}
for (int i = index; i < candidates.length; i++) {
if (candidates[i]<=target) {
List<Integer> list=new ArrayList<>(tempList);
list.add(candidates[i]);
getAnswers(res,candidates,target-candidates[i],list,i + 1);
} else {
break;
}
}
}
}
40. 组合总和 II leetcode JAVA的更多相关文章
- Java实现 LeetCode 40 组合总和 II(二)
40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- 40. 组合总和 II + 递归 + 回溯 + 记录路径
40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...
- 40组合总和II
题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...
- Leetcode题库——40.组合总和II
@author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...
- LeetCode 40. 组合总和 II(Combination Sum II)
题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...
- leetcode 40. 组合总和 II (python)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- 40. 组合总和 II
题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只 ...
- 组合总和 II
组合总和 II 题目介绍 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...
随机推荐
- js里面的三种注释方法
javascript(js)语言里面的注释方法有三种. 第一种是多行注释"/**/",一般js文件开头,介绍作者,函数等信息. /* *author:xxx *day:2008-0 ...
- delphi 三层架构简单例子(经测试成功)
delphi 三层架构简单例子(经测试成功) 转载 2013年12月19日 09:48:57 1100 所谓三层: (1) 客户端 (2) 服务器端 (3) 数据库 在数据访问时,使得客户端必须通过服 ...
- springboot 跳过单元测试
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-suref ...
- ReentrantLock(重入锁)功能详解和应用演示
目录 1. ReentrantLock简介 2. ReentrantLock和synchronized的相同点 2.1 ReentrantLock是独占锁且可重入的 3. ReentrantLock相 ...
- 【POJ1222】EXTENDED LIGHTS OUT
参考博客 https://blog.csdn.net/so_so_y/article/details/76098713 题意 有一些灯泡组成了5*6的方阵.每个开关(开关会使灯泡的状态发生变化)除了控 ...
- 100. Same Tree同样的树
[抄题]: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...
- Linux buffer and cache
A buffer is something that has yet to be "written" to disk. A cache is something that has ...
- if-return 语句
if(A > B): return A+1 return A-1 or if(A > B): return A+1 else: return A-1 +++++++++++++++++++ ...
- js失去焦点触发
onblur="displayRest($(this))"
- linux c MQTT客户端实现
linux c MQTT客户端实现 摘自:https://www.jianshu.com/p/d309de966379 一.前言:mqtt协议是轻量级的消息订阅和发布(publish/subscrib ...