LeetCode 040 Combination Sum II
题目要求: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.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- 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]
分析:
Combination Sum 里面的元素可以无限次使用,但是Combination Sum II每个元素只能使用一次。
代码如下:
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(candidates.begin(), candidates.end());
set<vector<int> > ans;
vector<int> record;
searchAns(ans, record, candidates, target, 0);
vector<vector<int> > temp;
for (set<vector<int> >::iterator it = ans.begin(); it != ans.end(); it++) {
temp.push_back(*it);
}
return temp;
}
private:
void searchAns(set<vector<int> > &ans, vector<int> &record, vector<int> &candidates, int target, int idx) {
if (target == 0) {
ans.insert(record);
return;
}
if (idx == candidates.size() || candidates[idx] > target) {
return;
}
for (int i = 1; i >= 0; i--) {
record.push_back(candidates[idx]);
}
for (int i = 1; i >= 0; i--) {
record.pop_back();
searchAns(ans, record, candidates, target - i * candidates[idx], idx + 1);
}
}
};
LeetCode 040 Combination Sum II的更多相关文章
- Java for LeetCode 040 Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [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】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 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 ...
随机推荐
- 如何在 Debian 9 上搭建 LNMP 环境
步骤一.安装Nginx Nginx在默认的Debian存储库中可用. 使用以下命令更新软件包索引并安装Nginx: sudo apt update sudo apt install nginx 安装过 ...
- [Luogu P2257] YY的GCD (莫比乌斯函数)
题面 传送门:洛咕 Solution 推到自闭,我好菜啊 显然,这题让我们求: \(\large \sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)\in prime]\) 根 ...
- hashmap源码面试分析
HashMap源码问题解析 问: 说一说对hash的理解 答: hash是对任意长度的输入输出为相同长度的输出 问: hash算法的问题 答: hash冲突问题 问: hash冲突是否可以避免 答: ...
- 深入研究Paxos算法原理
一.Paxos算法产生的背景 Paxos算法是基于消息传递且具有高度容错特性的一致性算法,是目前公认的解决分布式一致性问题最有效的算法之一,其解决的问题就是在分布式系统中如何就某个值(决议)达成一致. ...
- React react-redux props或state更新视图无法重新渲染问题
记录学习React时自己是如何挖坑把自己埋了的过程:children以及其它props被修改时相关组件无法重新渲染(做了两天) 父组件代码: class UserHome extends Compon ...
- 理解Task和和async await
本文将详解C#类当中的Task,以及异步函数async await和Task的关系 一.Task的前世今生 1.Thread 一开始我们需要创建线程的时候一般是通过Thread创建线程,一般常用创建线 ...
- W: Possible missing firmware /lib/firmware/i915/bxt_guc_ver8_7.bin for module i915
在执行sudo update-initramfs -u过程中 出现这个错误意思就是说少了固件,只要去下载放到/lib/firmware/i915文件夹下就好了. 下载链接如下: https://git ...
- 服务器断电导致的ORACLE异常 : ORA-00214 ORA-01033 ORA-01034 ORA-00172 ORA-27101
工作环境中的集群迁移之后,oracle出了挺多问题,最开始一直没找到原因,后来发现做迁移的人是冷迁移的,且数据库节点是硬关机的,惊了( 表现症状有不能登陆,登录了不能操作等 第一个报的是 ORA-00 ...
- 使用webapi绑定layui数据表格完整增删查改记录
因为每次给layui数据表格绑定数据或者类似操作的时候 总要重新做一遍 而且忘记很多东西 所以干脆写博客把相关东西记录下来 便于查阅和修正 以下是一个完整的数据表格i项目的增删改查案例 先来看后台 ...
- 关于 node build/build.js 卡顿的问题
今天也是奇了怪了,npm run dev 可以正常执行,但node build/build.js 就死活不行,卡住不动 实在不行,就有了"重启"万能大法,结果依然不行.实在无奈, ...