LeetCode OJ--Combination Sum **
https://oj.leetcode.com/problems/combination-sum/
给一列数,3 2 1 3 3 8 7 9 ,每个数可以重复多次,给target 7, 问可以加起来得7的所有组合。
递归,类似深搜的思想。
- class Solution {
- public:
- vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
- vector<vector<int> > ans;
- if(candidates.size()==)
- return ans;
- //remove duplicates
- sort(candidates.begin(),candidates.end());
- vector<int>::iterator itr = unique(candidates.begin(),candidates.end());
- candidates.resize(itr - candidates.begin());
- if(candidates[]>target)
- return ans;
- //remove the elements great than target
- int len = candidates.size();
- while(len>= && candidates[len-] > target)
- len--;
- candidates.resize(len);
- vector<int> ansPiece;
- combinationSum(candidates,ans,target,,ansPiece);
- return ans;
- }
- void combinationSum(vector<int> & candidates,vector<vector<int> > &ans,int target,int pivot,vector<int> ansPiece)
- {
- if(target == )
- {
- ans.push_back(ansPiece);
- return;
- }
- if( target < ||pivot == candidates.size())
- return;
- int i = ;
- while(target - i>=)
- {
- if(i != )
- ansPiece.push_back(candidates[pivot]);
- combinationSum(candidates,ans,target - i,pivot+,ansPiece);
- i = i + candidates[pivot];
- }
- }
- };
LeetCode OJ--Combination Sum **的更多相关文章
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 从Leetcode的Combination Sum系列谈起回溯法
在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...
- Leetcode 之 Combination Sum系列
39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...
随机推荐
- hadoop核心组件概述及hadoop集群的搭建
什么是hadoop? Hadoop 是 Apache 旗下的一个用 java 语言实现开源软件框架,是一个开发和运行处理大规模数据的软件平台.允许使用简单的编程模型在大量计算机集群上对大型数据集进行分 ...
- Python学习笔记:math模块(数学),random模块(随机数)
math模块 math模块用于数学意义上的一些计算,常用的方法有: math.pi:PI的值(3.141592653589793). math.floor(x):返回一个小于等于x的最大整数(浮点类型 ...
- HihoCoder - 1636 Pangu and Stones(区间DP)
有n堆石子,每次你可以把相邻的最少L堆,最多R堆合并成一堆. 问把所有石子合并成一堆石子的最少花费是多少. 如果不能合并,输出0. 石子合并的变种问题. 用dp[l][r][k]表示将 l 到 r 之 ...
- mysql不能登陆
前些天还正常运行的mysql,不知怎么就不能登陆了.错误提示为 :ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (1 ...
- cacheData
<%@ page language="java" import="java.util.*,com.fiberhome.bcs.appprocess.common.u ...
- logback mybatis 打印sql语句
logbac.xml 文件的基础配置参考的园友的 http://www.cnblogs.com/yuanermen/archive/2012/02/13/2349609.html 然后hibernat ...
- 将FragmentManger事务添加到返回栈中
FragmentManger事务添加或替换的 Fragment 后,这时点击 Back 键,程序并不会返回添加之前的状态. 我们可以使用 Transaction 对象的 addToBackStack( ...
- Codeforces 1139D(期望dp)
题意是模拟一个循环,一开始有一个空序列,之后每次循环: 1.从1到m中随机选出一个数字添加进去,每个数字被选的概率相同. 2.检查这个序列的gcd是否为1,如果为1则停止,若否则重复1操作直至gcd为 ...
- 菜鸟之路——机器学习之SVM分类器学习理解以及Python实现
SVM分类器里面的东西好多呀,碾压前两个.怪不得称之为深度学习出现之前表现最好的算法. 今天学到的也应该只是冰山一角,懂了SVM的一些原理.还得继续深入学习理解呢. 一些关键词: 超平面(hyper ...
- [oldboy-django][2深入django]分页功能
1 django自带分页 1.1 分页模板 <!DOCTYPE html> <html lang="en"> <head> <meta c ...