39. Combination Sum

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

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

Note:

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

For example, given candidate set [2, 3, 6, 7] and target 7,

My Thought

题目的大致意思:

给定一个非负整数的集合(不包含重复元素),以及给定一个目标数字 T,给出集合所有的子集,满足以下三个条件:

  • 该子集所有元素之和为目标数字 T
  • 每个子集允许元素重复
  • 不允许有相同的子集

给定的集合很规范:非负而且不包含重复元素。

看到数列先排序。这样按顺序遍历获得的解一定不重复。

想法:

从小到大排完列表后,递归求解。

我们要在对于 \(sorted\ list\) 范围 \([0,n-1]\)中求解子集满足题意。

记:

  • 目标整数记为 \(t\)
  • 求解过程为 \(find\)
  • 遍历数组C下标,记为 \(i\)

则递归形式:

\[find(i,t,n-1) = C[i] + find(i, t-C[i],n-1)
\]

这个递推公式包含了重复元素利用的情况(\(f(i,...)=C[i]+f(i,...)\))

伪代码:

sort(C);   // C范围:[0,n-1]
// vector v:用来暂存一个解
// begin:当前处理下标
PROCEDURE find(v,target,begin)
if target<C[index]
return
if binary_search(begin,n-1)!= FALSE
v.push(SN) //SN为二分搜索找到的元素
ret.push(v)
for i = beg to n-1 do
temp = v
temp.push(C[i])
find(temp, target-C[i],i)

Code(C++ 16ms)

class Solution {
public:
vector<vector<int>> ret;
vector<int> v;
// binary search
int bs(vector<int>&nums, int l,int h, int t){
if(l<=h){
int mid = (l+h)/2;
if(nums[mid]<t)
return bs(nums,mid+1,h,t);
else if(nums[mid]>t)
return bs(nums,l,mid-1,t);
return mid;
}
return -1;
}
bool find(vector<int> vv,int n,int beg){
if(n<v[beg])
return false;
int pos=bs(v,beg,v.size()-1,n);
vector<int > temp=vv;
if(pos!=-1){
temp.push_back(v[pos]);
ret.push_back(temp);
}
for(int i=beg;i<v.size();++i){
temp=vv;
temp.push_back(v[i]);
find(temp, n-v[i], i);
}
return false;
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
v = candidates;
vector<int> vv;
find(vv,target,0);
return ret;
}
};

LeetCode题解39.Combination Sum的更多相关文章

  1. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  2. 【LeetCode】39. Combination Sum (2 solutions)

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

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

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

  4. 【一天一道LeetCode】#39. Combination Sum

    一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...

  5. LeetCode OJ 39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  6. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

  7. 【LeetCode】39. Combination Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...

  8. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  9. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

随机推荐

  1. 算法工程师<机器学习基础>

    <机器学习基础> 逻辑回归,SVM,决策树 1.逻辑回归和SVM的区别是什么?各适用于解决什么问题? https://www.zhihu.com/question/24904422 2.L ...

  2. css属性之统一设置文本及div之间的对齐方式

    设为 Flex 布局以后,子元素的float.clear和vertical-align属性将失效.hdp-uf{ display: -webkit-box; /* 老版本语法: Safari, iOS ...

  3. import cv2出现“ImportError: DLL load failed: 找不到指定的模块”

    操作系统:windows server 2008 r2 enterprise 64位 Python版本:3.7.0 64位 这个问题坑了我一天,看了不少博客,用了好多方法,也没用.不多说了,介绍我的方 ...

  4. 对Inode、Hard Link以及Soft Link的理解

    一.EXT2/EXT3等文件系统的分区格式 Linux的文件系统从EXT2开始将文件的属性和文件的实际内容分开存储,文件的属性由inode存储,文件的内容由block存储. 系统在对磁盘进行分区格式化 ...

  5. Database Change Notification

    使用java监控oracle数据库的变化,主要是针对表数据,如果发生变化,使用select去查询,能够达到推送的目的 package com.test.notifi; import java.sql. ...

  6. SOUI视频教程

    第1讲-开发环境搭建 http://my.tv.sohu.com/us/282307706/89333168.shtml 第2讲- 基本应用程序框架代码解读 http://my.tv.sohu.com ...

  7. ImCash:币圈英文术语大全

    近年来随着数字货币的火热,在全世界范围内涌现出了一群数字货币的“发烧友”和忠实投资者,他们形成了自己的圈子“币圈”,并且有了自己的文化和语言,今天就让我们一起来了解一下外国币圈有哪些有意思的英文“专用 ...

  8. 周一干货~手把手教你安装 Visual Studio 安卓模拟器

    干货~手把手教你安装 Visual Studio 安卓模拟器 转 http://mini.eastday.com/mobile/171107134734194.html# 今天软妹为大家带来一篇来自M ...

  9. 组合 数论 莫比乌斯反演 hdu1695

    题解:https://blog.csdn.net/lixuepeng_001/article/details/50577932 题意:给定范围1-b和1-d求(i,j)=k的数对的数量 #includ ...

  10. 在Ubuntu下开启我的第一个Android工程

    目录 环境 流程 Phone PC端 我踩到坑 1.device offline Error while Installing APK 2.NO MODULE 3.应用无法安装 环境 小米Note4: ...