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)的更多相关文章

  1. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  2. 【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 ...

  3. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  4. LeetCode OJ 40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. 【LeetCode】40. Combination Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...

  6. LeetCode:12. Integer to Roman(Medium)

    1. 原题链接 https://leetcode.com/problems/integer-to-roman/description/ 2. 题目要求 (1) 将整数转换成罗马数字: (2) 整数的范 ...

  7. LeetCode 40. Combination Sum II (组合的和之二)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  8. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

  9. [LeetCode] Combination Sum II (递归)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. EOF及相关函数

    结论:EOF是在头文件stdio.h中预定义的一个宏,而eof(end of file)是一个与标准输入/输出流相关联的标志位.当文件指针已经指向文件尾且再次尝试读取时,eof标志会被设置.同时,某些 ...

  2. WAS部署 -- SRVE0255E: 尚未定义要处理 /snoop 的 Web 组/虚拟主机

    问题描述: URL:http://localhost:9080/pay: (中文)SRVE0255E: 尚未定义要处理 /snoop 的 Web 组/虚拟主机 访问出现以上错误. 找了很多资料,觉得说 ...

  3. pypy入门:pypy的安装及使用介绍

    在做python开发的人,应该或多或少的听说过一点pypy吧.我猜.所以就不做背景介绍了,有不懂的同学可以看看这里: 1.什么是pypy: http://www.360doc.com/content/ ...

  4. mybatis插入返回主键

     useGeneratedKeys="true" keyProperty="id" <insert id="insertReturnPrimar ...

  5. SignalR 教程一

    转帖官方教程:Tutorial: Getting Started with SignalR 2 and MVC 5 http://www.asp.net/signalr/overview/gettin ...

  6. Css中路径data:image/png;base64的用法详解 (转载)

    大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: background-image:url(data:image/png;base64, iVBORw0KGg ...

  7. Django开发BUG汇总

    使用版本知悉 limengjiedeMacBook-Pro:~ limengjie$ python --version Python :: Anaconda, Inc. limengjiedeMacB ...

  8. 统计 SQL Server中的 Job 情况

    SELECT J.name 作业名称, P.step_id'步骤编号',P.step_name'步骤名称',p.SubSystem '步骤类型',P.command'执行命令', CASE freq_ ...

  9. 来看看Uber的司机支持服务签到及预约系统的架构设计思路

    Uber的Greenlight Hubs(GLH)在全球拥有超过700个分支机构,为合作车主提供从账户和支付到车辆检查和车主注册等各方面的人工支持.为了给合作车主创造更好的体验并提高客户满意度,Ube ...

  10. iOS之创建通知、发送通知和移除通知的坑

    1.创建通知,最好在viewDidLoad的方法中创建 - (void)viewDidLoad { [super viewDidLoad]; //创建通知 [[NSNotificationCenter ...