【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums toT.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
For example, given candidate set 10,1,2,7,6,1,5
and target 8
,
A solution set is: [1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
跟Combination Sum的差别就是上面红色的那行字,只要把递归时候的起点由i改为i+1即可,参见下列代码中高亮的部分:
代码如下:
- public class Solution {
- private void combiDfs(int[] candidates,int target,List<List<Integer>> answer,List<Integer> numbers,int start){
- if(target == 0){
- answer.add(new ArrayList<Integer>(numbers));
- return;
- }
- int prev = -1;
- for(int i = start;i < candidates.length;i++){
- if(candidates[i] > target)
- break;
- if(prev != -1 && prev == candidates[i])
- continue;
- numbers.add(candidates[i]);
- combiDfs(candidates, target-candidates[i], answer, numbers,i+1);
- numbers.remove(numbers.size()-1);
- prev = candidates[i];
- }
- }
- public List<List<Integer>> combinationSum2(int[] candidates, int target) {
- List<List<Integer>> answer = new ArrayList<List<Integer>>();
- List<Integer> numbers = new ArrayList<Integer>();
- Arrays.sort(candidates);
- combiDfs(candidates, target, answer, numbers,0);
- return answer;
- }
- }
【leetcode刷题笔记】Combination Sum II的更多相关文章
- 【leetcode刷题笔记】Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【leetcode刷题笔记】Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode刷题笔记】Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode刷题笔记】N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
随机推荐
- 解决长时间计划任务rsync同步进程数过多
用rsync同步远程服务器,由于设置的的同步间隔较短(5分钟),这样一旦网速问题导致5分钟内同步不完.就会倒是同步紊乱,导致系统中很多rsync进程(# ps -aux | grep rsync) ...
- 方法return外部链接
return new ModelAndView(new RedirectView(url));
- java之CGLIB动态代理
© 版权声明:本文为博主原创文章,转载请注明出处 CGLIB动态代理: CGLIB动态代理就是对指定的类生成一个子类,覆盖其中所有的方法并环绕增强 优势: - 1. 业务类只需要关注业务逻辑本身,保证 ...
- CentOS 下Mysql数据库的安装与配置
一.mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常 的方便,在Linux上如果要安装数据库, ...
- github上比較好的开源项目(持续更新)
1:https://github.com/Skykai521/StickerCamera 实现相机功能 实现对图片进行裁剪的功能 图片的滤镜功能 能为图片加入贴纸(贴纸可移动,放大,旋转) 能为图片加 ...
- VSCode调试.net core 2.0 输出窗口乱码
Q:输出窗口乱码 A:修改.vscode文件夹下,tasks.json文件,具体内容见图
- 一个方便的图片载入框架——ImageViewEx
我的博客:http://mrfufufu.github.io/ 一.前言 近期在整理项目中的一些代码,以备即将开展的新项目中使用,刚刚整理到一个图片载入的 lib.用起来很的简单,和 picasso ...
- bzoj3174【TJOI2013】解救小矮人
3174: [Tjoi2013]解救小矮人 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 573 Solved: 293 [Submit][Stat ...
- springmvc上传方法
/** * * @param file 上传的文件 * @param filePath 上传到那个目录 * @return 上传后的文件名字 * @throws IOException */ publ ...
- 【windows7 + Appium】之Appium安装以及其他工具安装配置
首先感谢虫师总结的教程:<appium新手入门>.以及:<appium新手入门(2)—— 安装 Android SDK> 目录: 安装Appium&安装node.js ...