题目要求: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. 同时使用mybatis和mybatis-plus时,pageHelper失效问题解决

    一.问题由来 最近刚拿到一个别人的项目,该项目中使用mybatis和mybatis-plus来操作数据库,我们需要在此基础上添加新功能. 做功能开发时一切都很顺利,我也很快完成了自己负责的模块,然后和 ...

  2. curl 錯誤碼

    CURL状态码列表 状态码 状态原因 解释 0 正常访问 1 错误的协议 未支持的协议.此版cURL 不支持这一协议. 2 初始化代码失败 初始化失败. 3 URL格式不正确 URL 格式错误.语法不 ...

  3. Nginx 配置请求响应时间

    1.常见默认nginx.conf配置日志格式 log_format main '$remote_addr - $remote_user [$time_local] "$request&quo ...

  4. [Luogu P2051] [AHOI2009]中国象棋 (状压DP->网格DP)

    题面 传送门:https://www.luogu.org/problemnew/show/P2051 Solution 看到这题,我们不妨先看一下数据范围 30pt:n,m<=6 显然搜索,直接 ...

  5. Stimulsoft Reports和Dashboards发布新版本2020.5具有多项改进

    Stimulsoft仪表工具实现所需的数据可视化和自己的信息图表.该产品能够应用必要的过滤器和排序,汇总数据,执行任何复杂度的计算.该产品的优势在于其多功能性-能够为您的业务,财务,销售,行业等任何领 ...

  6. ZJU-Summer Camp Problem

    Day 1 NTT #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define m_k make_pair #define mod 99 ...

  7. Java_大体介绍(超级短的那种)

    Java三大版本 Java SE: Java Standard Edition, 定位于客户端, 用于桌面应用软件编程 Java ME: Java Micro Edition, 用于嵌入式系统开发 J ...

  8. canvas基础[一]探究出初中数学知识

    何时用SVG何时用canvas SVG 矢量图,视觉清晰,文件小 <svg viewBox="0 0 100 100"> <circle cx="50& ...

  9. c# ToolStrip控件图片和文字显示--原创

    如上图达到这样的效果 首先我们给属性Image和Text分别赋予需要显示的图片和文字 然后设置DisplyStyle属性为ImageAndText,意为同时显示图片和文字 各种设置ImageAlign ...

  10. excel导出csv包括逗号等的处理

    /** * @Title: trimRubbishChar * @Description: 导出的时候需要对一格的内容进行检查,看是否有非法字符,以免串行 * @Since: 2016年8月2日 下午 ...