combination sum && combination sum II
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的更多相关文章
- 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 ...
- Path Sum,Path Sum II
Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a ...
- 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 ...
- LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解
文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. ...
- 关于数论分块里r=sum/(sum/l)的证明!
今天的模拟赛里T2要使用到数论分块,里面有一个重要的坎就是关于r=sum/(sum/l)的证明,网上关于这道题的题解里都没有关于这个的证明,那么我就来填补一下: 在以下的文章里,我都会使用lo(x)表 ...
- 有两个数组a,b,大小都为n;通过交换a,b中的元素,使sum(a)-sum(b)最小。
今天在浏览网页的时候,发现了一个叫做 华为面试题(8分钟写出代码) 的链接,不确定真实性,纯属好奇,就点进去看看 这个可能是很老的题目吧,因为我看到这题目时,底下有好多评论了.提到XX排序,内存占用 ...
- 有两个数组a,b,大小都为n,;通过交换a,b中的元素,使sum(a)-sum(b)最小。
有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小. 当前数组a和数组b的和之差为 A = sum(a) - ...
- 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 ...
- [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 ...
- 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) ...
随机推荐
- Python3 常用模块1
目录 os模块 对文件夹操作 对文件进行操作 sys模块 json 和pickle模块 logging模块 日志等级 longging模块的四大组件 自定义配置 os模块 通过os模块我们可以与操作系 ...
- 通过EF操作Sqlite时遇到的问题及解决方法
1.使用Guid作为字段类型时,能存,能查,但是作为查询条件时查询不到数据 解决方法:连接字符串加上;binaryguid=False
- kubernetes-集群构建
本实验参考:https://github.com/gjmzj/kubeasz kubernetes官方github地址 https://github.com/kubernetes/kubernetes ...
- 小白的springboot之路(十)、全局异常处理
0.前言 任何系统,我们不会傻傻的在每一个地方进行异常捕获和处理,整个系统一般我们会在一个的地方统一进行异常处理,spring boot全局异常处理很简单: 介绍前先说点题外话,我们现在开发系统,都是 ...
- ConcurrentHashMap比其他并发集合的安全效率要高一些?
前言 我们知道,ConcurrentHashmap(1.8)这个并发集合框架是线程安全的,当你看到源码的get操作时,会发现get操作全程是没有加任何锁的,这也是这篇博文讨论的问题——为什么它不需要加 ...
- python学习-class对象
# 面向对象 python,java,c## 面向过程 C # 类和对象# 类 类型类别.类别 物以类聚 一类事物# 班级.人类.动物类.车.学生类.老师类.手机.电脑# 统称 == 共同特性# 不具 ...
- 跟着文档学习gulp1.2创建任务(task)
导出任务 任务(task)可以分为公开(public)或私有(private)类型 公开任务从gulpfile中被导出(export),可以通过gulp命令直接调用 私有任务被设计为在内部使用,通常作 ...
- 《Java基础知识》Java类与类之间的关系
类与类之间最常见的关系主要有三种:依赖(uses-a).聚合(has-a)和继承(is-a). 下面以在线书店订单系统为例,来详细的讲述这三种关系的概念. 在线书店订单系统的主要功能是:注册用户可以登 ...
- Python是什么?你真的了解Python吗?
一.Python是什么相信混迹IT界的很多朋友都知道,Python是近年来最火的一个热点,没有之一.从性质上来讲它和我们熟知的C.java.php等没有什么本质的区别,也是一种开发语言,而且已经进阶到 ...
- Winform中实现自定义屏保效果(附代码下载)
场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建form ...