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) ...
随机推荐
- Python如何实现微信群万人同步直播?
很多人传言微信网页版(https://wx.qq.com/)接口已经被封了,所以所有的微信都不能登录网页版,这是错误的. 2019年7月微信对网页版微信进行了动态安全策略调整,导致一大批微信号不能登录 ...
- Vue中无法检测到数组的变动
本周在写项目中遇到修改数组中的值时,视图无变化问题.在查阅Vue官方文档后了解到,由于由JavaScript 的限制,Vue 不能检测以下数组的变动: 当利用索引直接设置一个数组项时,例如:vm.it ...
- 手把手教你看懂并理解Arduino PID控制库——引子
介绍 本文主要依托于Brett Beauregard大神针对Arduino平台撰写的PID控制库Arduino PID Library及其对应的帮助博客Improving the Beginner’s ...
- EFK的搭建(未完成)
EFK 是ELK 日志分析的一个变种,能够更好的来实现日志分析. 首先我们先准备3台 centos7的服务器,在给他们调成2核2G的状态打开. 软件 版本号 zookeeper 3.4.14 Kafk ...
- Java并发之synchronized关键字深度解析(二)
前言 本文继续[Java并发之synchronized关键字深度解析(一)]一文而来,着重介绍synchronized几种锁的特性. 一.对象头结构及锁状态标识 synchronized关键字是如何实 ...
- Spire.Cloud 在线编辑
简介 Spire.Cloud在线编辑器是一款基于网页的 Office 文件编辑工具,支持在网页中打开.编辑.打印 Word.Excel.PPT 文件,支持将文档保存到私有云盘.支持 IE.Chrome ...
- 2、MVC+IOC容器+ORM结合
1.常规写法,难道我们每次都new一个服务,如下面的UserService和CompanyService然后调用服务的Find方法去操作,为什么我们不让UserService和CompanyServi ...
- 拖动条(SeekBar)的功能与用法
拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程度,而拖动条则通过滑块的位置来标识数值——而且拖动条允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,比如调节音量等 ...
- PMBOK 指南 第二章 项目运行环境
2.1概述 事业环境因素(EEF)源于项目外部(往往企业外部) 组织过程资产(OPA)源于企业内部 2.2 事业环境因素 项目团队不能控制 2.2.1 组织内部的事业环境因素 组织文化.结构和治理 设 ...
- git提交时忽略指定文件
git提交时忽略指定文件 我们在项目开发过程中经常用到git来管理自己的项目,使用git版本控制进行多人协作开发具有许多优势,这里就不一一阐述了,有兴趣的同学可以自己去查找资料进行系统的学习.而本篇文 ...