#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 著作权归领扣网络所有.商业转载请联系 ...
随机推荐
- Python 底层原理知识
1.Python是如何进行内存管理的? 答:从三个方面来说,一对象的引用计数机制,二垃圾回收机制,三内存池机制 一.对象的引用计数机制 Python内部使用引用计数,来保持追踪内存中的对象,所有对象都 ...
- Ubuntu中网络配置interfaces与界面网络配置NetworkManager
[Server版本] 在Ubuntu Server版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改 /etc/network/interfaces .具体设置方法如下: (1) U ...
- frame shiro 认证示例及原理简述
shiro 认证流程 1.创建一个 javaSE 的maven项目(quickstart),并添加依赖 <dependency> <groupId>junit</grou ...
- jsoncpp cmake
(1)下载jsoncpp源码源码地址:https://github.com/open-source-parsers/jsoncpp/tree/0.y.z(2)解压源码 unzip jsoncpp-0. ...
- JS + jQuery 实现元素自动滚动到底部,兼容IE、FF、Chrome
HTML代码: <ul class="tasklog-dialog-ul" id="auto_to_bottom"> <li>删除虚拟机 ...
- ASP.NET中使用UpdatePanel时用Response输出出现错误的解决方法
asp.net中执行到Response.write("xx");之类语句或Microsoft JScript 运行时错误: Sys.WebForms.PageRequestMana ...
- 在 Linux 上创建虚拟机规模集和部署高度可用的应用
利用虚拟机规模集,可以部署和管理一组相同的.自动缩放的虚拟机. 可以手动缩放规模集中的 VM 数,也可以定义规则,以便根据资源使用情况(如 CPU 使用率.内存需求或网络流量)进行自动缩放. 在本教程 ...
- svn回退到具体的版本
svn回退到具体的版本 找到项目的版本号 命令行中输入相关命令 到指定地点找到项目即可
- 使用FBTweak
使用FBTweak https://github.com/facebook/Tweaks FBTweak是Facebook的一款开源库,用于微调数据而无需我们重复编译跑真机用的,它支持4种类型的cel ...
- Python学习---IO的异步[twisted模块]
安装twisted模块 Linux: pip3 install twisted Window: a. http://www.lfd.uci.edu/~gohlke/pythonlibs/#twiste ...