给定一个数组 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]
]
【2,2,3,6,7】 target=8 按上一题的代码,会出现 [[2,6][2,6]]故当候选集中相邻两个位置的值相等时,跳过。
 class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList();
if(candidates.length == 0 || candidates == null)return res;
Arrays.sort(candidates);
helper(res,new ArrayList(),candidates,target,0);
return res;
}
public void helper(List<List<Integer>> res,List<Integer> list,int[] candidates,int target,int start){
if(target < 0)return;
if(target == 0){
res.add(new ArrayList(list));
return;
}
for(int i = start;i < candidates.length;i++){
if(i != start && candidates[i] == candidates[i - 1]) continue;
list.add(candidates[i]);
helper(res,list,candidates,target - candidates[i],i+1);
list.remove(list.size() - 1);
}
}
}

2019-05-01 20:32:34

LeetCode--040--组合总和 II(java)的更多相关文章

  1. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  2. LeetCode 中级 - 组合总和II(105)

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  3. LeetCode 40. 组合总和 II(Combination Sum II)

    题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...

  4. leetcode 40. 组合总和 II (python)

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  5. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  6. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  7. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  8. [LeetCode] 39. 组合总和

    题目链接 : https://leetcode-cn.com/problems/combination-sum/ 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ...

  9. 40. 组合总和 II + 递归 + 回溯 + 记录路径

    40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...

  10. 组合总和 II

    组合总和 II 题目介绍 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...

随机推荐

  1. ORACLE查询隐含参数

    查询隐含参数:col name for a30col VALUE for a10col DESCRIB for a40set lines 200SELECT x.ksppinm NAME, y.ksp ...

  2. 第三方框架:EventBus

    1 研发背景 案例:我们在主页点收藏按钮,未登录状态,跳登录界面,在登录界面跳注册页面,注册成功,关闭注册页面,关闭登录页面,回到主页,刷新item列表和登录状态. 我们一般会用到发送广播和接收广播来 ...

  3. 【服务器】一次对Close_Wait 状态故障的排查经历

    最近接连听说一台线上服务器总是不响应客户端请求. 登录服务器后查询iis状态,发现应用程序池状态变为已停止. 按经验想,重启后应该就ok,第一次遇到也确实起了作用,当时完全没在意,以为是其他人无意把服 ...

  4. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_02 泛型_3_定义和使用含有泛型的类

    创建一个类,添加一个name的属性,然后生成get和set 使用上面创建的类 使用泛型 所以我们取出来也是一个Object的类型 定义的时候规定的类型是Integer,所以这里setName设置的时候 ...

  5. python学习笔记(数据类型)

    python数据类型: int 类型 float 小数类型 string 字符串 布尔类型 a = True b = False 1.列表,也称数组或list或array.它的表达方式通过下标或索引或 ...

  6. Linux 服务器安全优化

    最小的权限+最少的服务=最大的安全 所以,无论是配置任何服务器,我们都必须把不用的服务关闭.把系统权限设置到最小,这样才能保证服务器最大的安全.下面是CentOS服务器安全设置,供大家参考. 一.注释 ...

  7. X509格式的证书校验(基于GMSSL2019-06-15版本)

    实现X509格式证书的链式校验 // cert_public.cpp : Defines the exported functions for the DLL application. // #inc ...

  8. struts2 基础

    框架(frameWork):某一种应用的半成品 struts2: 表现层 处理与页面进行交互的相关功能  hibernate: 持久层 负责业务逻辑数据的持久化  spring: 业务层 负责复杂的业 ...

  9. 修改属性item1(1变化)

    给imgList1,7,12,16添加数据 数据层data:{imgList1:[],imgList7:[],imgList12:[],imgList16:[],} 处理层let _this=this ...

  10. layui中获取全部提交的数据

    <form class="layui-form" action="">...........input textarea ......</fo ...