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. ARTS-S pytorch中backward函数的gradient参数作用

    导数偏导数的数学定义 参考资料1和2中对导数偏导数的定义都非常明确.导数和偏导数都是函数对自变量而言.从数学定义上讲,求导或者求偏导只有函数对自变量,其余任何情况都是错的.但是很多机器学习的资料和开源 ...

  2. windows程序设计04_显示汉字的16进制

    看下面的代码 //utf-8编码 #include<stdio.h> int main() { char a[] = "中国"; printf("%x\n&q ...

  3. 从零开始的openGL——四、纹理贴图与n次B样条曲线

    前言 在上篇文章中,介绍了如何加载绘制模型以及鼠标交互的实现,并且遗留了个问题,就是没有模型表面没有纹理,看起来很丑.这篇文章将介绍如何贴纹理,以及曲线的绘制. 纹理贴图 纹理加载 既然是贴图,那首先 ...

  4. Day 03 作业

    简述变量的组成 变量名,赋值符号,变量值 简述变量名的命名规范 变量名应该能反映变量值所描述的状态 变量名必须以字母数字下划线组合且不能以数字开头 变量名不能是关键字 简述注释的作用 让后面的代码失效 ...

  5. vue 双语言切换中,data内翻译文字不正常切换的解决方案

    背景 有这么一个登录页面,相关功能如下: 支持双语言,点击切换语言 表单内部有一个自定义的select,里面option的label.value都是的名字由外部提供:其中预设的option的label ...

  6. node+express 配置安装以及数据解析,cookie,session

    一.express安装,创建服务 (1)安装:npm install express --save(2)创建服务 server.js: const express = require('express ...

  7. java基础面向对象总结(一)

    年底了,总结下知识点和遇到过的面试题目. 1,如何理解面相对象. ‘万物皆对象’说的没错,听起来挺反感的,问一个说一个.有些话觉得用自己的话讲出来肯俗点,但可以证明你理解了一点.我理解的是:之所以叫面 ...

  8. rabbitmq~消息失败后重试达到 TTL放到死信队列(事务型消息补偿机制)

    这是一个基于消息的分布式事务的一部分,主要通过消息来实现,生产者把消息发到队列后,由消费方去执行剩下的逻辑,而当消费方处理失败后,我们需要进行重试,即为了最现数据的最终一致性,在rabbitmq里,它 ...

  9. WPF特点

    前言:为什么要学习WPF呢?因为随着现阶段硬件技术的升级以及客户对体验的要求越来越高,传统的GDI和USERS(或者是GDI+.USERS)已经不能满足这个需求,因此,WPF技术应运而生. WPF的特 ...

  10. C#程序编写高质量代码改善的157个建议【16-19】[动态数组、循环遍历、对象集合初始化]

    前言   软件开发过程中,不可避免会用到集合,C#中的集合表现为数组和若干集合类.不管是数组还是集合类,它们都有各自的优缺点.如何使用好集合是我们在开发过程中必须掌握的技巧.不要小看这些技巧,一旦在开 ...