[array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium
descrition
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]
]
解析
和 leetcode - 39. Combination Sum - Medium 类似,只是这里的数组元素存在重复,并且元素不可重复取。
代码只实现了其中一种递归形式,这样的实现方法递归层数应该是最浅的。
code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution{
public:
vector<vector<int> > combinationSum2(vector<int> &candidates, int target){
vector<vector<int> > ans;
vector<int> vecCur;
sort(candidates.begin(), candidates.end());
combinationSum2Backtracking(candidates, 0, vecCur, target, ans);
return ans;
}
void combinationSum2Backtracking(vector<int>& candidates, int index,
vector<int>& vecCur, int target,
vector<vector<int> > &ans){
if(target < 0)
return;
if(target == 0){
if(!vecCur.empty())
ans.push_back(vecCur);
return;
}
for(int i=index; i<candidates.size(); i++){
if(candidates[i] > target) // candidates mush in ascending order
break;
// choose candidates[i], and each number in candidates may only
// be used onece in combination
vecCur.push_back(candidates[i]);
combinationSum2Backtracking(candidates, i+1, vecCur, target - candidates[i], ans);
// don't choose candidates[i]
vecCur.pop_back();
// skip the duplicate
while((i+1)<candidates.size() && candidates[i+1] == candidates[i])
i++;
// after i++, i will point to a new unique number
}
}
};
int main()
{
return 0;
}
[array] leetcode - 40. Combination Sum II - Medium的更多相关文章
- [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 ...
- 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
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 ...
- Java [Leetcode 40]Combination Sum II
题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
随机推荐
- Memcached 及 Redis 架构分析和区别比较
Memcached和Redis作为两种Inmemory的key-value数据库,在设计和思想方面有着很多共通的地方,功能和应用方面在很多场合下(作为分布式缓存服务器使用等) 也很相似,在这里把两者放 ...
- 【SSM之旅】Spring+SpringMVC+MyBatis+Bootstrap整合基础篇(一)项目简介及技术选型相关介绍
试水 一直想去搭建个自己的个人博客,苦于自己的技术有限,然后也个人也比较懒散.想动而不能动,想动而懒得动,就这么一直拖到了现在.总觉得应该把这几年来的所学总结一番,这样才能有所成长. 不知在何时,那就 ...
- MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作)
MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作): a.创建2张表 create table userinfo(nid int not nul ...
- Servlet之初始化参数和传递数据(ServletConfig,ServletContext )
ServletConfig 容器初始化一个Servlet的时候,会为这个Servlet建一个唯一的Servletconfig的对象(Servlet的配置对象) 容器会从部署的描述文件(web.xml) ...
- Elasticsearch java api 基本搜索部分详解
文档是结合几个博客整理出来的,内容大部分为转载内容.在使用过程中,对一些疑问点进行了整理与解析. Elasticsearch java api 基本搜索部分详解 ElasticSearch 常用的查询 ...
- Linux常用基础命令
一.系统目录结构 约定俗成: bin (binaries)存放二进制可执行文件 sbin (super user binaries)存放二进制可执行文件,只有root才能访问 etc (e ...
- 一步步搭建Retrofit+RxJava+MVP网络请求框架(二),个人认为这次封装比较强大了
在前面已经初步封装了一个MVP的网络请求框架,那只是个雏形,还有很多功能不完善,现在进一步进行封装.添加了网络请求时的等待框,retrofit中添加了日志打印拦截器,添加了token拦截器,并且对Da ...
- 在Linux中使用线程
我并不假定你会使用Linux的线程,所以在这里就简单的介绍一下.如果你之前有过多线程方面的编程经验,完全可以忽略本文的内容,因为它非常的初级. 首先说明一下,在Linux编写多线程程序需要包含头文件p ...
- 双向bfs-八数码问题
[八数码][1] [1]: https://www.luogu.org/problem/show?pid=1379 其实除了搜索恶心一点,好像也没什么提高+的 bfs搜的是状态. 双向bfs同时从起点 ...
- 小白的Python之路 day1 用户输入
1 2 3 4 5 6 7 8 #!/usr/bin/env python #_*_coding:utf-8_*_ username =input("username:") p ...