#leetcode刷题之路40-组合总和 II
给定一个数组 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的更多相关文章
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- LeetCode刷题笔记-回溯法-组合总和问题
题目描述: <组合总和问题>给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. cand ...
- #leetcode刷题之路39-组合总和
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的数字可以无限制重复被选取 ...
- #leetcode刷题之路45-跳跃游戏 II
给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例:输入: [2,3,1,1,4]输出: 2 ...
- 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 ...
- Java实现 LeetCode 40 组合总和 II(二)
40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...
- 40. 组合总和 II + 递归 + 回溯 + 记录路径
40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...
- 40组合总和II
题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...
- 刷题-力扣-113. 路径总和 II
113. 路径总和 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/path-sum-ii 著作权归领扣网络所有.商业转载请联系 ...
随机推荐
- Android MVP Plugin,一键完成MVP结构代码编写
推荐一个Gradle的学习系列,Gradle相关的知识一直很匮乏,难得发现一个不错的系列: http://www.cnblogs.com/davenkin/p/gradle-learning-1.ht ...
- centos7 yum安装mysql | mariaDb
mysql解释: mysql数据库是最常用的一种数据库,下面我来在centos7的迷你版上安装一下mysql.绝对纯净的环境哦 centos: CentOS-7-x86_64-Minimal-1 ...
- LeetCode题解之Single Number
1.题目描述 2.分析 3.代码 int singleNumber(vector<int>& nums) { map<int,int> m; for( vector&l ...
- Asp.net mvc Kendo UI Grid的使用(二)
上一篇文章对Kendo UI做了一些简单的介绍以及基本环境,这篇文章来介绍一下Grid的使用 先上效果图: 要实现这个效果在Controller在要先导入Kendo.Mvc.UI,Kendo.Mvc. ...
- lock free数据结构内存回收技术-hazard pointer
lock free数据结构一般来说拥有比基于lock实现的数据结构更高的性能,但是其实现比基于lock的实现更为复杂,需要处理的难题包括预防ABA问题,内存如何重用和回收等.通常,最简单最有效的处理A ...
- C# 实现水印
直接上源码 public class WaterTextBox : TextBox { //private const int EM_SETCUEBANNER = 0x1501; //[DllImpo ...
- [翻译] UPCardsCarousel
UPCardsCarousel UPCardsCarousel is a carousel with a cards based UI for iOS. UPCardsCarousel是一个旋转木马效 ...
- Git提交代码自动触发JenKins构建项目
1.需求场景 用户提交代码后自动触发jenkins构建项目 流程图如下: 2.JenKins安装Gitlab Hook Plugin插件 3.JenKins配置 4.Gitlab Hook Plugi ...
- 《C++ Primer Plus》读书笔记之八—对象和类
第十章 对象和类 1.面向对象编程(OOP)的特性:抽象.封装和数据隐藏.多态.继承.代码的重用性. 2.指定基本类型完成了3项工作:①决定数据对象需要的内存数量.②决定如何解释内存中的位(lon ...
- (1)基于tcp协议的编程模型 (2)tcp协议和udp协议的比较 (3)基于udp协议的编程模型 (4)反射机制
1.基于tcp协议的编程模型(重中之重)1.1 编程模型服务器: (1)创建ServerSocket类型的对象,并提供端口号: (2)等待客户端的连接请求,调用accept()方法: (3)使用输入输 ...