1.Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]

Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

我的解答:

class Solution {

public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> temp;
//if (candidates.size() < 1)return res;
sort(candidates.begin(),candidates.end());
combinationSum(candidates,res,temp,target,);
return res;
} private:
void combinationSum(vector<int>& candidates, vector<vector<int>> &res,vector<int> &temp, int target,int begin)
{
if (!target)
{
res.push_back(temp);
return ;
}
// 这里要注意了,&& 两边应该先判断 i ,再判断candidates[i],否则将导致数组越界!!!
for (int i = begin; i != candidates.size() && target >= candidates[i]; ++i)
{
temp.push_back(candidates[i]);
combinationSum(candidates,res,temp,target - candidates[i],i);
temp.pop_back();
}
}
};

其中有一个要注意的地方:

&& 运算符:先判断左边,只有左边为真,才计算右边

|| 运算符:先判断左边,只有左边为假,才计算右边

2.Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

Each number in candidates may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

Example 2:

Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
  [1,2,2],
  [5]
]
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> candi;
sort(candidates.begin(),candidates.end());
findNext(candidates,target,,candi,res);
return res;
} void findNext(vector<int> &candidates, int target,int begin,vector<int> &candi,vector<vector<int>> &res)
{
if (!target)
{
res.push_back(candi);
return;
}
for (int i = begin;i < candidates.size() && target >= candidates[i];++i)
{
if (i == begin || candidates[i] != candidates[i - ]){
candi.push_back(candidates[i]);
findNext(candidates,target - candidates[i],i+,candi,res);
candi.pop_back();
} }
}
};

combination sum && combination sum II的更多相关文章

  1. 32. Path Sum && Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

  2. Path Sum,Path Sum II

    Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...

  3. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

  4. LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解

    文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. ...

  5. 关于数论分块里r=sum/(sum/l)的证明!

    今天的模拟赛里T2要使用到数论分块,里面有一个重要的坎就是关于r=sum/(sum/l)的证明,网上关于这道题的题解里都没有关于这个的证明,那么我就来填补一下: 在以下的文章里,我都会使用lo(x)表 ...

  6. 有两个数组a,b,大小都为n;通过交换a,b中的元素,使sum(a)-sum(b)最小。

    今天在浏览网页的时候,发现了一个叫做  华为面试题(8分钟写出代码) 的链接,不确定真实性,纯属好奇,就点进去看看 这个可能是很老的题目吧,因为我看到这题目时,底下有好多评论了.提到XX排序,内存占用 ...

  7. 有两个数组a,b,大小都为n,;通过交换a,b中的元素,使sum(a)-sum(b)最小。

    有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小. 当前数组a和数组b的和之差为    A = sum(a) - ...

  8. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

  9. [Count the numbers satisfying (m + sum(m) + sum(sum(m))) equals to N]

    Given an integer N, the task is to find out the count of numbers M that satisfy the condition M + su ...

  10. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

随机推荐

  1. C# 利用反射更改父类公开对象

    需求 : 有一个保存数据库字段的基础类,现在要加个状态返回给前端,但是又不能改基础类: class BaseA { public string Name { get; set; } } class A ...

  2. Django 10

    目录 cookie和session cookie session token Django中间件 自定义中间件 process_request process_response 其他方法 cookie ...

  3. 【CSS】381- 提升你的CSS选择器技巧

    我已经使用CSS多年了,但直到最近我才深入研究了一下CSS选择器. 我为什么要这样做呢?我们都知道选择器,但麻烦的是随着时间的推移,很容易习惯于在每个项目中使用相同的可信任选择器来实现你需要做的事情. ...

  4. JQuery 操作checkbox

    获取checkbox选中的状态 deleteAll全选的name 1. $("input[name='deleteAll']").is(":checked") ...

  5. SSM-配置tkmybatis

    引言 Mybatis 与 Hibernate的一个很大的区别就是Mybatis所有的数据库操作语句都需要自己写,对于简单的单表操作来说是比较烦琐的.因此有人就开发了tk.mybatis插件,通过这个插 ...

  6. django基础之day09,创建一个forms表单组件进行表单校验,知识点:error_messages,label,required,invalid,局部钩子函数,全局钩子函数, forms_obj.cleaned_data,forms_obj.errors,locals(), {{ forms.label }}:{{ forms }},{{ forms.errors.0 }}

    利用forms表单组件进行表单校验,完成用户名,密码,确认密码,邮箱功能的校验 该作业包含了下面的知识点: error_messages,label,required,invalid,局部钩子函数,全 ...

  7. sql为什么用0,1表示男女?在sql语句里转好还是在页面转好?

    转化语句:SELECT CASE `user_gender` WHEN '1' THEN '男' WHEN '0' THEN '未知'ELSE '女' END AS gender FROM `info ...

  8. 用故事说透 HTTPS

    本文来自素燕公众号,原文地址:用故事说透 HTTPS 故事中的主演:小华今年上大一,这是她第一次离开父母,独自一人到北京上学.今天妈妈的生日,想了想要给妈妈一个祝福,便给妈妈发了条消息:妈妈收到这条消 ...

  9. c++-纯虚函数和抽象类

    纯虚函数和抽象类 C面向接口编程和C多态 函数类型语法基础 函数指针做函数参数(回调函数)思想剖析 函数指针做函数参数两种用法(正向调用.反向调用) 纯虚函数 抽象类 抽象类基本概念 抽象类在多继承中 ...

  10. php使用微信登录

    1.第一步 $hosturl = urlencode('');//异步回调地址 $wechatInfo = WechatInfo::get_wechat(); //查询appid $url = &qu ...