博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]--40. Combination Sum II
阅读量:7091 次
发布时间:2019-06-28

本文共 1260 字,大约阅读时间需要 4 分钟。

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]]

跟上一篇思想其实是一样的,只不过这个不能用同一水平线上即不能重复一个元素。上代码,递归自己写出来,看来还是懂了,嘿嘿。

public class Solution {
public List
> combinationSum2(int[] candidates, int target) { List
> res = new ArrayList
>(); if (candidates.length == 0) return res; List
list = new ArrayList
(); Arrays.sort(candidates); findSum(candidates, list, 0, 0, target, res); return res; } private void findSum(int[] candidates, List
list, int sum, int level, int target, List
> res) { if (sum == target) { if (!res.contains(list)) res.add(new ArrayList
(list)); return; } else if (sum > target) return; else for (int i = level; i < candidates.length; i++) { list.add(candidates[i]); findSum(candidates, list, sum + candidates[i], i+1, target, res); list.remove(list.size() - 1); } }}

转载地址:http://wisql.baihongyu.com/

你可能感兴趣的文章
Unity3D如何减少安装包大小
查看>>
漫游Kafka设计篇之数据持久化
查看>>
Java提高篇——equals()与hashCode()方法详解
查看>>
【python】——小程序之电话薄
查看>>
Atitit.iso格式蓝光 BDMV 结构说明
查看>>
MySQL的create table as 与 like区别(转)
查看>>
Linux学习历程(持续更新整理中)
查看>>
Linux查看物理CPU个数、核数、逻辑CPU个数
查看>>
软件设计模式详解:OCP原则
查看>>
Apache服务器常规操作
查看>>
qt cef嵌入web
查看>>
Java程序员面试失败的5大原因
查看>>
过滤器(Filter)
查看>>
外观模式
查看>>
Webmin|Linux管理员远程管理工具
查看>>
【温故而知新-Javascript】比较 undefined 和 null 值
查看>>
CentOS中iptables防火墙 开放80端口方法
查看>>
Kafka 在行动:7步实现从RDBMS到Hadoop的实时流传输
查看>>
[内核]Linux workqueue
查看>>
云计算开始。。。
查看>>