LeetCode:40. Combination Sum II(Medium)
1. 原题链接
https://leetcode.com/problems/combination-sum-ii/description/
2. 题目要求
给定一个整型数组candidates[ ]和目标值target,找出数组中累加之后等于target的所有元素组合
注意:(1)每个可能的答案中,数组中的每一个元素只能使用一次;(2)数组存在重复元素;(3)数组中都是正整数;(4)不能存在重复解
3. 解题思路
这与第39题 Combination Sum 看起来很是类似,但一些细节要求完全不同,因此处理起来的方法也不相同。相同的是依旧采用递归的方法。
不存在重复解,但给定的数组中存在重复元素,因此先对candidates[ ]进行排序,方便处理重复元素的问题
4. 代码实现
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class CombinationSum40 {
public static void main(String[] args) {
CombinationSum40 cs = new CombinationSum40();
int[] candidates = {10, 1, 2, 7, 6, 1, 5};
for (List l : cs.combinationSum2(candidates, 8))
System.out.println(l);
} public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
for(int x:candidates)
System.out.print(x+" ");
System.out.println();
List<List<Integer>> result = new ArrayList<List<Integer>>();
combinationSum(result, new ArrayList<Integer>(), candidates, 0, target);
return result; } public void combinationSum(List<List<Integer>> result, List<Integer> tmp, int[] candidates, int start, int target) {
if (target > 0) {
for (int i = start; i < candidates.length; i++) {
if (i > start && candidates[i] == candidates[i - 1]) // 避免重复的结果
continue; // 结束for循环中其后的语句,跳回for循环
tmp.add(candidates[i]);
combinationSum(result, tmp, candidates, i + 1, target - candidates[i]);
tmp.remove(tmp.size() - 1); // 累加和超过target,则删去列表表尾元素
}
}
if (target == 0) {
result.add(new ArrayList<>(tmp));
} }
}
LeetCode:40. Combination Sum II(Medium)的更多相关文章
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- LeetCode OJ 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- LeetCode:12. Integer to Roman(Medium)
1. 原题链接 https://leetcode.com/problems/integer-to-roman/description/ 2. 题目要求 (1) 将整数转换成罗马数字: (2) 整数的范 ...
- 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(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
- [LeetCode] Combination Sum II (递归)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- MongoDB限制记录数
MongoDB limit()方法 要限制 MongoDB 中返回的记录数,需要使用limit()方法. 该方法接受一个数字类型参数,它是要显示的文档数. 语法 limit()方法的基本语法如下: & ...
- Github 删除 repository
Github 删除 repository 如下图操作
- android开发者您还在为模拟器犯愁吗?神级android模拟器---Genymotion一个更快、接近完美的模拟器……
摘要:Android系统非常特别,App须要进行模拟化測试.即使这样仍然有解决的办法---虚拟化技术. 之前的模拟器比方eclipse自带的是非常慢的一种,并且模拟器的版本号并非最新的.开机.能够说差 ...
- 阿里云linux服务器登录失败,Connection closed
ssh_exchange_identification: read: Connection reset by peer报错如下: [root@izbp17x1~]# ssh admin@139.196 ...
- OC报错,after command failed: Directory not empty
Directory not empty这个错误经常出现,出现的原因也很多,今天主要记录一下楼主自己碰到的这种情况. 全部错误提示: error: couldn't remove ‘路径/app-fzy ...
- 二十二、详述 IntelliJ IDEA 中恢复代码的方法
在咱们正常开发项目的时候,难免遇到在开发过程中由于某种原因,想要将代码恢复到前一版本的情景.特别是在咱们删除了某些代码,想要恢复之前删除的代码的时候,了解这个在 IntelliJ IDEA 中恢复代码 ...
- HTML5对表单的一些有意思的改进
HTML5对表单进行了许多的改进,在这篇文章中,我选择了其中个人认为很有趣的三个变化:自动聚焦,对button元素的改进,把表单元素与非父元素的form表单挂钩进行简单的介绍. 1. 自动聚焦 自动聚 ...
- java之sleep(),join(),yield(),wait(),notify()、notifyAll()区别
1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...
- Git--将服务器代码更新到本地
1. git status(查看本地分支文件信息,确保更新时不产生冲突) 2. git checkout -- [file name] (若文件有修改,可以还原到最初状态; 若文件需要更新到服务器上, ...
- 分别利用(代码,Xib,SB)创建空的App工程
1. 利用代码: 2.利用XIB: 3.利用Storyboard: Xcode 7默认该方式创建项目工程,就不必多说了!