LeetCode 40. 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 to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- 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]
- ]
题目标签:Array
这道题目和前面那一题本质一摸一样,不同之处是,这题每一个数字只能用一次,之前那题可以无限用;这一题给的array里有重复的,之前那题没有。所以只要之前那题做了,稍微改动一下就可以了。把递归下去的数字index 从i 改成 i + 1,因为这里不需要重复的数字了。再有就是要设一个条件,如果一个重复的数字,不是出现在第一位的话,就跳过它。
Java Solution:
Runtime beats 83.86%
完成日期:07/17/2017
关键词:Array
关键点:Backtracking with sorted array
- public class Solution
- {
- public List<List<Integer>> combinationSum2(int[] candidates, int target)
- {
- List<List<Integer>> list = new ArrayList<>();
- Arrays.sort(candidates);
- backtrack(list, new ArrayList<>(), candidates, target, 0);
- return list;
- }
- public boolean backtrack(List<List<Integer>> list, List<Integer> tempList,
- int[] nums, int remain, int start)
- {
- if(remain < 0) // if remain is 0 or less than 0, meaning the rest numbers are even greater
- return false; // therefore, no need to continue the loop, return false
- else if(remain == 0)
- {
- list.add(new ArrayList<>(tempList));
- return false;
- }
- else
- {
- for(int i=start; i<nums.length; i++)
- {
- if(i > start && nums[i] == nums[i-1])
- continue;
- boolean flag;
- tempList.add(nums[i]);
- flag = backtrack(list, tempList, nums, remain - nums[i], i+1); // i + 1 because we cannot use same number.
- tempList.remove(tempList.size() - 1);
- if(!flag) // if find a sum or fail to find a sum, there is no need to continue
- break;// because it is a sorted array with no duplicates, the rest numbers are even greater.
- }
- return true; // return true because previous tempList didn't find a sum or fail a sum
- }
- }
- }
参考资料:
http://www.cnblogs.com/grandyang/p/4419386.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 40. Combination Sum II (组合的和之二)的更多相关文章
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
- 【LeetCode】Combination Sum II(组合总和 II)
这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...
- 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 --- java
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...
随机推荐
- 基于图形检测API(shape detection API)的人脸检测
原文:https://paul.kinlan.me/face-detection/ 在 Google 开发者峰会中,谷歌成员 Miguel Casas-Sanchez 跟我说:"嘿 Paul ...
- 浅谈JAVA中“增强”类的某个方法的几个中方法!
一.继承 使用场景:能够控制这个类的构造的时候,才可以使用继承. 优点:简单容易使用, 缺点:耦合性大大的增强,不利于后期的维护,所以对于继承这种方法,谨慎使用. 代码实现:二.装饰者模式 使用场景 ...
- WEB前端面试真题 - 2000!大数的阶乘如何计算?
HTML5学堂-码匠:求某个数字的阶乘,很难吗?看上去这道题异常简单,却不曾想里面暗藏杀机,让不少前端面试的英雄好汉折戟沉沙. 面试真题题目 如何求"大数"的阶乘(如1000的阶乘 ...
- 自学Unity3D 之 贪吃蛇 添加摄像机跟随
在Unity的世界中, 物体的位置都是由向量构成的. 今天所需要做的就是让摄像机保持跟蛇头的相对距离. 首先 设蛇头的位置在A 点 , 摄像机的位置在B 点 则 我们可以知道 他们的offse ...
- [android游戏开发初学]SurfaceView初探之缓冲区测试
先上测试代码 private static class PathView extends SurfaceView implements SurfaceHolder.Callback{ private ...
- Maven 整合strut与Hibernate,获取不到Session
struts使用的是2.3.24 Hibernate使用的5.0.7 注意hebernate一定要在struts之前申明,不然容易出现500错误, <project xmlns="ht ...
- C的函数指针与指针函数
1.函数指针 指向函数的指针.本质是一个指针. 指针变量可以指向变量的地址.数组.字符串.动态分配地址,同时也可指向一个函数,每个函数在编译的时候,系统会分配给该函数一个入口地址,函数名表示这个入口地 ...
- clipboard.js 介绍
这是著名开源项目 clipboard.js 的 README.md,我把它翻译成中文.发出来,方便自己和他人阅读. 项目地址:https://github.com/zenorocha/clipboar ...
- codeforces 8c Looking for Order
https://vjudge.net/problem/CodeForces-8C 题意: 一个平面上放着许多东西,每个东西都有一个坐标,最开始一个人在一个起始坐标,她出发去拿东西,一次要么拿一件东西, ...
- Apache shiro的简单介绍与使用(与spring整合使用)
apache shiro框架简介 Apache Shiro是一个强大而灵活的开源安全框架,它能够干净利落地处理身份认证,授权,企业会话管理和加密.现在,使用Apache Shiro的人越来越多,因为它 ...