leetcode个人题解——#40 Combination Sum2
思路:解法和39题类似,改动了两处:
1、因为题目要求每个元素只能出现一次(不代表每个数只能有一个,因为数据中会有重复的数字),所以代码中21行搜索时下一次循环的位置+1;
2、将临时存放答案的vector<vector<int>>类型改为set<vector<int>>类型,避免重复。
class Solution {
public:
int size = ;
set<vector<int>> ans;
void search(int pos, vector<int>& candidates, int target, vector<int>& res, int sums){
if(sums == target){
ans.insert(res);
return;
}
if (pos >= size) return;
for(int i = pos; i < size; i++)
{
if(candidates[i] > target) return;
sums += candidates[i];
res.push_back(candidates[i]);
if(sums > target){
sums -= candidates[i];
res.pop_back();
return;
}
search(i + , candidates, target, res, sums);
sums -= candidates[i];
res.pop_back();
} } vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
size = candidates.size();
vector<int> res;
search(, candidates, target, res, );
vector<vector<int>> out(ans.begin(),ans.end());
return out;
}
};
leetcode个人题解——#40 Combination Sum2的更多相关文章
- leetcode个人题解——#39 Combination Sum
思路:先对数据进行排序(看评论给的测试数据好像都是有序数组了,但题目里没有给出这个条件),然后回溯加剪枝即可. class Solution { public: ; vector<vector& ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
- LeetCode 中等题解(4)
40 组合总和 II Question 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- spring boot 配置Rabbit
单独安装Rabbit服务并设置启动,可以通过浏览器访问,一般访问地址是http://localhost:15672/ ,用户名密码看配置文件的用户名密码 1 实例化配置类注解 import org.s ...
- II、Python HelloWorld
大家都不是小孩子了,直接上 IDE 现在有个大问题!!没有解析器啊 这样 解析器地址比他多个 e OJBK
- UML架构设计师必备神器
UML-架构设计师必备神器 做过Java开发的一定都听过UML,也都能感觉到它的重要性.由其是在网上搜索一些高级技术介绍,写的好的.阅读量高的.让初.中级程序员容易看懂的.思路清晰的文章一定有UML类 ...
- Git很简单--图解攻略
Git Git 是目前世界上最先进的分布式版本控制系统(没有之一) 作用 源代码管理 为什么要进行源代码管理? 方便多人协同开发 方便版本控制 Git管理源代码特点 1.Git是分布式管理.服务器和客 ...
- 基于 HTML5 Canvas 的 Web SCADA 组态电机控制面板
前言 HT For Web 提供完整的基于 HTML5 图形界面组件库.您可以轻松构建现代化的,跨桌面和移动终端的企业应用,无需担忧跨平台兼容性,及触屏手势交互等棘手问题.也可用于快速创建和部署,高度 ...
- 使用css完成物流进度的样式
使用css完成物流进度的样式 效果: css样式: <style type="text/css"> ul li { list-style: none; } .packa ...
- Java Hibernate Validator
Hibernate Validator是Hibernate提供的一个开源框架,使用注解方式非常方便的实现服务端的数据校验. 官网:http://hibernate.org/validator/ hib ...
- centos6.8安装mysql过程
1.验证Centos是否安装MySQL $>yum list installed | grep mysql 2.删除MySql $>yum –y remove mysql-libs.X86 ...
- 《Act with Prudence》读后感
<97 Things Every Should Know>中第一个编程方面的建议 文章链接:行事谨慎 很赞同文章中的观点,在做项目中是要谨慎行事和考虑后果.一直在项目前期考虑不够周到,以至 ...
- stm32 IO口八种模式区别
初学STM32,遇到I/O口八种模式的介绍,网上查了一下资料,下面简明写出这几种模式的区别,有不对的地方请大家多多指正! 上拉输入模式:区别在于没有输入信号的时候默认输入高电平(因为有弱上拉).下拉输 ...