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 ...
随机推荐
- 记2019年目标之一没有996的大数据分析BI实战历程
本文略长,阅读大约需要10分钟. 懵懵懂懂的学习了python,然后一发不可收拾的爱上了python大数据分析,慢慢的走进了大数据的学堂,学习如何大数据挖掘,大数据分析,到BI系统建设使用. 大数据的 ...
- 【.net开发者自学java系列】使用Eclipse开发SpringMVC(3)
[.net开发者自学java系列]使用Eclipse开发SpringMVC(3) 标签(空格分隔): Spring RESTful 很久没继续学习java的spring了.接下来继续 回忆一下上个随笔 ...
- oracle数据库每天自动备份
现在介绍一下我们项目中使用exp工具每天自动对oracle进行备份. 用这个工具我们可以使用命令行的方式也可以使用pl/sql developer来进行导出. 我们要使用exp命令来导出oracle的 ...
- jQuery实现全选、全不选以及反选操作
在写购物车案例时实现全选操作使用的是js的getAttribute()setAttribute()方法获取checked属性的值是undefined实现完成之后全选操作,如果在全选中的情况下改变其中一 ...
- 基于jQuery的轮播焦点图图
轮播焦点图 ——仿淘宝首页jquery轮播焦点图,我特意去taobao首页看了下它的轮播,好像有点相似,我不保证是我写的这样. 本例来源:站长之家http://sc.chinaz.com/jiaobe ...
- 大数据学习--day16(集合总体架构--ArrayList--LinkedList)
集合总体架构--ArrayList--LinkedList Collection接口的实现类用法上都有相似的方法.Map同理. List: 特性 : 1. 有索引 2. 有序 ...
- 编写Makefile规则
一个工程中的源文件不计其数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作 ...
- django的Cookie-9
设置Cookie 可以通过HttpResponse对象中的set_cookie方法来设置cookie. HttpResponse.set_cookie(cookie名字, value=cookie值, ...
- 20155327李百乾 Exp3 免杀原理与实践
20155327李百乾 Exp3 免杀原理与实践 实践guocheng 一.Msfvenom使用编码器 1.利用(virustota)[https://www.virustotal.com/]检测实验 ...
- linux挂在samba服务器到本地(用于备份文件到nas或者windows的文件服务器)
1.安装工具 首先在linux上安装samba访问工具 sudo apt-get install smbclient sudo apt-get install cifs-utils 2.查看服务器目录 ...