给定一个数组 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. 使用 Leaflet 显示 ArcGIS 生成西安80坐标的地图缓存

    Leaflet 是一个非常小巧灵活的 Geo js 库,esri 本身也在 Github 上有 leaflet 的相关项目.但是 leaflet 本身支持 Web Mercator Auxiliary ...

  2. 安装部署OpenPAI + VSCode 提交

    ========================================================== 安装openpai请参考这篇 https://www.cnblogs.com/ji ...

  3. hornor8改user模式为debug模式

    在学习Android软件安全的过程中,经常要用到Android的动态调试.但是呢,一般的Android应用在发布的时候都是发布版的不能直接被调试,为了能使Android应用能够支持调试就需要对Andr ...

  4. centos7 yum安装mysql | mariaDb

    mysql解释: mysql数据库是最常用的一种数据库,下面我来在centos7的迷你版上安装一下mysql.绝对纯净的环境哦 centos:    CentOS-7-x86_64-Minimal-1 ...

  5. 单独配置 Ehcache

    package com.shy.ehcache; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net ...

  6. Entity Framework之DB First方式

    EF(Entity Framework的简称,下同)有三种方式,分别是:DataBase First. Model First和Code First. 下面是Db First的方式: 1. 数据库库中 ...

  7. String使用equals和==比较的区别

    "=="  操作符的作用: 1.用于基本数据类型的比较 2.判断引用是否指向堆内存的同一块地址. equals的作用: 用于判断两个变量是否是对同一个对象的引用,即堆中的内容是否相 ...

  8. C# 输入一个整数,求质因数

    质数,质因数 应该都了解,在这里不过多解释,直接上代码: List<int> results = new List<int>(); int number = Int32.Par ...

  9. Forefront TMG 之 ISP 冗余传输链路(ISP-R)

    在 Forefront TMG 中,新增了ISP 冗余传输链路功能:在 TMG 中,你可以同时使用两条活动的外部链路,使用模式分为以下两种: 故障转移模式:在主要链路工作正常的情况下,所有的流量都通过 ...

  10. 修改Linux SSH连接端口和禁用IP,安装DDoS deflate

    测试系统:centos7 修改连接端口 修改配置文件 vi /etc/ssh/sshd_config 去掉port 22的注释,添加新的端口配置 port your_port_num 自定义端口选择建 ...