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 ...
随机推荐
- DAVINCI开发原理
本文中约定: [host] 表示主机PC机Linux [target] 表示目标板Linux DAVINCI开发原理之一----ARM端开发环境的建立(DVEVM) 1. 对DAVINCI平台,TI在 ...
- python要点之III
[python要点之III] 1.实现交换. 在C/C++中,交换两个变量,需要2个变量,tmp=x;x=y;y=tmp;. 在python中,交换两个变量可以这么写:x,y=y,x. 2.is&am ...
- 高性能Web服务器Nginx的配置与部署研究(9)核心模块之HTTP模块基本常用指令
一.HTTP模块的作用是什么? Nginx的HTTP模块用于控制Nginx的HTTP进程. 二.指令 1. alias 含义:指定location使用的路径,与root类似,但不改变文件的跟路径,仅适 ...
- css的优先级和权重问题 以及!important优先级
一,前言: 刚加的css怎么没有渲染出来?浏览器中查看,发现是被其他的css给覆盖了,相信我们都曾遇到过这样的问题.那么浏览器是如何选择css标签的渲染顺序的呢?换句话说,css选择器的优先级是怎么规 ...
- sqlserver计算日期
在网上找到的一篇文章,相当不错哦O(∩_∩)O~ 这是计算一个月第一天的SQL 脚本: SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一 ...
- Lucas–Kanade光流算法学习
Lucas–Kanade光流算法是一种两帧差分的光流估计算法.它由Bruce D. Lucas 和 Takeo Kanade提出. 光流(Optical flow or optic f ...
- 阿里云WindowsServer2012安装IIS失败
本文地址:http://www.cnblogs.com/drfxiaoliuzi/p/6388417.html 首先,向微软官方论坛的大神致敬: https://social.technet.micr ...
- 设置ctp文件按html文件解析
- Linux 设置默认编辑器(以nano为例)
查看nano地址 which nano output: /usr/bin/nano 设置默认编辑器 nano ~/.bashrc export EDITOR=nano alias vi=/usr/bi ...
- Spring框架总结(五)
自动装配(了解) 根据名称自动装配:autowire="byName" 自动去IOC容器中找与属性名同名的引用的对象,并自动注入 延续使用user.dao.service.acti ...