https://leetcode.com/problems/combination-sum-ii/

题目跟前面几道题很类似,直接写代码:

class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
if(candidates.size()==)
return res;
vector<int> temp;
helper(candidates,,target,temp);
return res; }
private:
void helper(vector<int>& candidates,int index,int target,vector<int>& temp){
if(target==)
res.push_back(temp); if(target< || index>=candidates.size()){
return;
} for(int i=index;i<candidates.size();i++)
{
if(i>index && candidates[i]==candidates[i-])//必须保证第一个位置以一个数值递归下去时不能重复!但第一次遍历时又必须保存,所以必须写成i>index,而不是i>0!!就是针对这类情形 1,1,2,5,6,7,10 8 【1,2,5】 【1,2,5】 【1,1,6】
continue;
temp.push_back(candidates[i]);
helper(candidates,i+,target-candidates[i],temp);//注意这个地方就不应该是index了,而应该传入i+1,否则,会出现降序数列
temp.pop_back();
}
}
private:
vector<vector<int>> res;
};

https://leetcode.com/problems/combinations/

class Solution {
public:
vector<vector<int>> combine(int n, int k) {
if(n<k)
return res;
vector<int> temp;
helper(n,,k,temp,k);
return res;
} void helper(int n,int pos,int k,vector<int>& temp,int count){
if(==count){
res.push_back(temp);
return;
}
if(pos>n||count<)
return;
for(int i=pos;i<=n;i++){
temp.push_back(i);
helper(n,i+,k,temp,--count);//这个地方写成i+1而不是pos+1
temp.pop_back();
count++;
}
} private:
vector<vector<int>> res;
};

Combination Sum II Combinations的更多相关文章

  1. 【leetcode】Combination Sum II

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

  2. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

  3. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

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

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

  5. 【LeetCode】40. Combination Sum II (2 solutions)

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

  6. LeetCode: Combination Sum II 解题报告

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

  7. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  8. leetcode-combination sum and combination sum II

    Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combi ...

  9. LeetCode 040 Combination Sum II

    题目要求:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find al ...

随机推荐

  1. 循序渐进Python3(十二) --0--  web之框架

    web框架的本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:utf ...

  2. 循序渐进Python3(十一) --1-- web之css

    css样式: css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化,CSS的可以使页面更加的美观. 基本上所有的html页面都或多或少的使用css.   ...

  3. crontab 系列

    crontab是一个很方便的在unix/linux系统上定时(循环)执行某个任务的程序使用cron服务,用 service crond status 查看 cron服务状态,如果没有启动则 servi ...

  4. oracle常用命令大全及心得

    学习时整理的 Oracle 1.set linesize 100; 设置长度2.set pagesize 30; 设置每页显示数目3.em a.sql 打开记事本 4.@ a 执行文件a中的代码,可指 ...

  5. C#读写XML

    1.根据xml格式的string生成xml文件并保存到本地 /// <summary> /// 保存XML到磁盘 /// </summary> /// <param na ...

  6. 又一种XML的解析方法

    [Fact(DisplayName="用户名为空")] public void Should_UsernameEmpty() { var paras = new Dictionar ...

  7. C++读写文件ofstream,ifstream,fstream)[转]

    在看C++编程思想中,每个练习基本都是使用ofstream,ifstream,fstream,以前粗略知道其用法和含义,在看了几位大牛的博文后,进行整理和总结: 这里主要是讨论fstream的内容:[ ...

  8. UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等

    (1)可以根据需要设置文本框的样式(包括形状.边框颜色.背景等). (2)可以根据需要设置文字显示样式(包括输入密码时的密文显示.文字横向居中.纵向居中上下.输入的文字是否首席木大写.文字超过后是否缩 ...

  9. Oracle Recommended Patches -- "Oracle JavaVM Component Database PSU" (OJVM PSU) Patches (文档 ID 1929745.1)

    From: https://support.oracle.com What is "Oracle JavaVM Component Database PSU" ? Oracle J ...

  10. Spring MVC CORS support

    使用详见: https://spring.io/blog/2015/06/08/cors-support-in-spring-framework 简单用法,在Controller 方法上加 @Cros ...