给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
说明:
所有数字(包括目标数)都是正整数。
解集不能包含重复的组合。

示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]

也就是把之前的39题加一个避免重复而已:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int i=; void recurrent(vector<int>& candidates,int target,int addr,vector<vector<int>>& ans,vector<int>& temp)//addr为当前遍历到的位置,
{
if(target==)
{
// if(temp.size()==1)
// cout<<temp[0]<<endl;
// if(temp.size()==3)
// cout<<temp[0]<<temp[1]<<temp[2]<<endl;
if(find(ans.begin(),ans.end(),temp)==ans.end()) ans.push_back(temp);//避免重复
return;
}
if(target<) return;
for(int i=addr;i<candidates.size();i++)
{
target-=candidates[i];
temp.push_back(candidates[i]);
recurrent(candidates,target,i+,ans,temp);
temp.pop_back();//还原,以继续遍历
target+=candidates[i];
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> ans;
int count=candidates.size();
if(count==||target<) return ans;
vector<int> temp;
sort(candidates.begin(),candidates.end());//排序,避免重复
recurrent(candidates,target,,ans,temp);
return ans;
} int main() {
vector<int> candidates={,,,,};
vector<vector<int>> ans=combinationSum2(candidates,);
std::cout << ans.size()<< std::endl;
return ;
}

#leetcode刷题之路40-组合总和 II的更多相关文章

  1. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  2. LeetCode刷题笔记-回溯法-组合总和问题

    题目描述: <组合总和问题>给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. cand ...

  3. #leetcode刷题之路39-组合总和

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的数字可以无限制重复被选取 ...

  4. #leetcode刷题之路45-跳跃游戏 II

    给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例:输入: [2,3,1,1,4]输出: 2 ...

  5. leetcode 刷题之路 66 Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  6. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  7. 40. 组合总和 II + 递归 + 回溯 + 记录路径

    40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...

  8. 40组合总和II

    题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...

  9. 刷题-力扣-113. 路径总和 II

    113. 路径总和 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/path-sum-ii 著作权归领扣网络所有.商业转载请联系 ...

随机推荐

  1. 对layoutInflater的理解

    参考该博客:http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html LayoutInflater是一个抽象类,通过调用其实例方法infl ...

  2. vue项目部署上线

    前言 今天把自己写的demo登录写完了,就想着试着走一下部署上线的流程.参考了很多的文档,终于成功进行了部署.在这里将服务器的搭建和vue项目的 部署上线进行整理(都是基础的知识,希望对大家有帮助.对 ...

  3. rman备份控制文件

    rman备份控制文件分为两种情况. 1.自动备份需要配置configure controlfile autobackup on配置之后,控制文件和spfile会在每次rman备份之后自动备份contr ...

  4. Centos 使用C++11 编译

    今天编译代码,发现使用auto后无法编译,我的当前linux内核版本:(4.7之后即可支持C++11) 这时,在编译末尾加入 -std=c++11 就可以正常编译了.如:

  5. 观察者模式 - Java 实现1(使用JDK内置的Observer模式)

    使用JDK内置的观察者模式 1. 可观察者(主题) 被观察的主题继承 Observable 对象, 使用该对象的调用 notifyObservers() 或 notifyObservers(arg) ...

  6. 最常见到的runtime exception 异常

    ArithmeticException, 算术异常ArrayStoreException, 将数组类型不兼容的值赋值给数组元素时抛出的异常BufferOverflowException, 缓冲区溢出异 ...

  7. mac os maverick下安装nginx+php-fpm via homebrew

    自己虽然平时爱折腾,却很少有记下来的习惯,除非碰到特别多问题的部署才会有冲动.今天看同事折腾git,在旁边看着他mac上的evernote满满的记了好几篇,全是技术相关的记录,忽然很感慨.过去解决了很 ...

  8. linux smem 查看各进程使用memory情况

    SMEM(8) SMEM(8) NAME smem - Report memory usage with shared memory divided proportionally. SYNOPSIS ...

  9. C#获取文件路径的几种方法

    //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称. string str5=Application.StartupPath;//可获得当前执行的exe的文件名. string str1 ...

  10. 审计系统---paramiko模块学习

    paramiko模块学习 [更多参考]http://www.cnblogs.com/wupeiqi/articles/4963027.html [paramiko的Demo实例]https://git ...