题目要求: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的更多相关文章

  1. Java for LeetCode 040 Combination Sum II

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

  2. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  3. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  4. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  6. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  7. 【LeetCode】040. Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

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

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

  9. Leetcode 40. Combination Sum II

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

随机推荐

  1. php socket通信的简单实现

    socket通信的原理在这里就不说了,它的用途还是比较广泛的,我们可以使用socket来做一个API接口出来,也可以使用socket来实现两个程序之间的通信,我们来研究一下在php里面如何实现sock ...

  2. 我的 Redis 被入侵了

    好吧,我也做了回标题党,像我这么细心的同学,怎么可能让服务器被入侵呢? 其实是这样的,昨天我和一个朋友聊天,他说他自己有一台云服务器运行了 Redis 数据库,有一天突然发现数据库里的数据全没了,只剩 ...

  3. 使用sql导出数据_mysql

    在mysql中 使用sql 脚本导出数据的方式之一: select * from table_name where x=y  INFO OUTFILE "/tmp/table_name.tx ...

  4. OGG投递进程报错无法open文件,无法正常投递

    1.1现象 之前有个客户遇到一个问题,OGG同步数据链路,突然有一天网络出现问题,导致OGG投递进程无法正常投递,无法写入目标端的该文件. 猜测是由于网络丢包等原因导致文件损坏,无法正常open,re ...

  5. 剑指Offer-Python(11-15)

    11.二进制中1的个数 链接:https://www.nowcoder.com/questionTerminal/8ee967e43c2c4ec193b040ea7fbb10b8?answerType ...

  6. 为什么人们总是认为epoll 效率比select高!!!!!!

    今天看公司代码时,发现代码里面使用的事清一色的代码使用epoll, 所以就得说一说了:宏观看一看epoll 和select的实现: select原理概述 调用select时,会发生以下事情: 从用户空 ...

  7. 适合 Go 新手学习的开源项目——在 GitHub 学编程

    作者:HelloGitHub-小鱼干&卤蛋 故事要从 2007 年说起.因为受够了 C++ 煎熬的 Google 首席软件工程师 Rob Pike 召集 Robert Griesemer 和 ...

  8. App与小程序对接

    背景: 商品详情页,点击分享,分享到微信好友,点开链接App拉起小程序. 用户在小程序浏览完成,跳转至原App购买商品. 功能点: 实现APP与小程序互调. 前提: 已对接好友盟ShareSDK(需要 ...

  9. mysql建立索引,实际工作中建立索引的示例

    1.根据业务场景建立相应的组合索引,一般是在主键,外键,常用来筛选查询的字段,按照字段之间组合的紧密程度,建立一定顺序的索引. 例如:为 t_org_exam_join_member_day  建立索 ...

  10. Redis在springboot项目的使用

    一.在pom.xml配置redis依赖 <!-- redis客户端代码 --> <dependency> <groupId>org.springframework. ...