思路:解法和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的更多相关文章

  1. leetcode个人题解——#39 Combination Sum

    思路:先对数据进行排序(看评论给的测试数据好像都是有序数组了,但题目里没有给出这个条件),然后回溯加剪枝即可. class Solution { public: ; vector<vector& ...

  2. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  3. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  4. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  5. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  6. Leetcode 简略题解 - 共567题

    Leetcode 简略题解 - 共567题     写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...

  7. LeetCode 中等题解(4)

    40 组合总和 II Question 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...

  8. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  9. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

随机推荐

  1. crobtab不执行定时任务的原因及解决办法

    服务未能启动或者权限问题.路径问题,网上很多种解决办法,就不多的说了. 1.查看crond日志: cat /var/log/cron 刚开始我的日志里面并没有执行写的脚本. 原因在于在脚本开始没有写s ...

  2. 20181101noip模拟赛T1

    思路: 我们看到这道题,可以一眼想到一维差分 但这样的复杂度是O(nq)的,显然会T 那么怎么优化呢? 我们会发现,差分的时候,在r~r+l-1的范围内 差分增加的值横坐标相同,纵坐标递增 减小的值横 ...

  3. CentOS7.6离线安装Redis5.0.4

    安装gcc-c++: 检查是否存在gcc-c++:rpm -qa|grep gcc-c++ 如果不存在就下载Linux-GC-C++文件: 访问镜像网站:http://mirrors.aliyun.c ...

  4. 【gitlab平台的搭建】

    gitlab同github相同,具有把源码集中存放的功能,同时依靠git进行code的同步,在实际的开发过程中可保证团队的项目同步,同时便于便于维护等 #下载这个rpm包 #gitlab.rb访问地址 ...

  5. Delphi Android下包含第三方DEX

    1.将jar转换为dex call dx --dex -verbose --output=.\output\dex\test_classes.dex --positions=lines .\outpu ...

  6. 使用docker-compose运行微服务项目#eureka+config+auth+gateway+module

    微服务架构中我们使用了必须的四个组件,eureka config gateway auth 其中config依赖eureka,auth依赖前两者,gateway又依赖auth 这样就确定了四个组件的启 ...

  7. leetcode-746-Min Cost Climbing Stairs(动态规划)

    题目描述: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...

  8. Enable CSS active pseudo styles in Mobile Safari

    http://alxgbsn.co.uk/2011/10/17/enable-css-active-pseudo-styles-in-mobile-safari/ document.addEventL ...

  9. 20155311 《Java程序设计》实验四 (Android程序设计)实验报告

    20155311 <Java程序设计>实验四 (Android程序设计)实验报告 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android.组 ...

  10. 20155317 实验二 Java面向对象程序设计

    20155317 实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...