Combination Sum [LeetCode]
Problem Description: http://oj.leetcode.com/problems/combination-sum/
Basic idea: It seems complicate at first. But once I got the recursive idea, the solution was neat. This code could be not efficient. I will rewrite with dynamic programming next time.
- class Solution {
- public:
- vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
- // Note: The Solution object is instantiated only once and is reused by each test case.
- std::sort(candidates.begin(), candidates.end());
- vector<vector<int> > combinations;
- for(int i = ; i < candidates.size(); i ++) {
- if(candidates[i] > target)
- break;
- if(candidates[i] == target) {
- vector<int> combination;
- combination.push_back(candidates[i]);
- combinations.push_back(combination);
- }else{
- if(i == && target % candidates[i] != )
- continue;
- if(i == && target % candidates[i] == ){
- vector<int> combination(target / candidates[i], candidates[i]);
- combinations.push_back(combination);
- continue;
- }
- vector<int> sub_candidates(candidates.begin(), candidates.begin() + i + );
- vector<vector<int> > rests = combinationSum(sub_candidates, target - candidates[i]);
- for(auto item: rests){
- item.push_back(candidates[i]);
- combinations.push_back(item);
- }
- }
- }
- return combinations;
- }
- };
Combination Sum [LeetCode]的更多相关文章
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
- Combination Sum —— LeetCode
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Combination Sum leetcode java
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)
根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 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 ...
随机推荐
- 【转载】.NET面试题系列[0] - 写在前面
原文:.NET面试题系列[0] - 写在前面 索引: .NET框架基础知识[1] - .NET框架基础知识(1) http://www.cnblogs.com/haoyifei/p/5643689.h ...
- Creating, Stopping, Re-Starting and Deleting a Timer in Oracle Forms
I have written many posts previously on Timers in Oracle Forms like how to change images randomly wi ...
- MYSQL写入数据时报错ERROR 1366 (HY000): Incorrect string value: '\xE8\x8B\xB1\xE5\xAF\xB8...' for c 插入中文不能插入
先把原先你创建的这个表删除,然后 CREATE TABLE IF NOT EXISTS tdb_goods( goods_id SMALLINT UNSIGNED PRIMARY KEY AUTO_I ...
- [转]-用Gradle 构建你的android程序
出处:http://www.cnblogs.com/youxilua 前言 android gradle 的插件终于把混淆代码的task集成进去了,加上最近,android studio 用的是gr ...
- 个人阅读作业 The Last
对于软件工程M1/M2的总结: 假象-MO 在团队开发的前期,我感觉自己其实给了自己很多的期待,因为一直希望着自己可以在团队中担任一个角色,用自己的力量为团队多做事情,也给了其他人一些假象,那就是看起 ...
- Nginx模块学习之————accesskey权限模块使用(简单的m3u8防盗链)
配置文件:http://www.cnblogs.com/tinywan/p/5983694.html 通过加密后的文件: 正确地址:curl -i http://访问的IP地址(这里是直播节点IP地址 ...
- Redis基础知识之————使用技巧(持续更新中.....)
一.key 设计技巧 把表名转换为key前缀 如, tag: 第2段放置用于区分区key的字段--对应mysql中的主键的列名,如userid 第3段放置主键值,如2,3,4...., a , b , ...
- DZY Loves Chessboard
DescriptionDZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m ...
- (二)程序中的内存&&栈
一.程序运行为什么需要内存?基本概念? 内存是程序运行的立足之地,程序需要用内存来存储一些变量. 内存管理最终是由操作系统完成的,内存在本质上是一个硬件器件,由硬件系统提供:内存由操作系统统一管理,为 ...
- UIButton(在代码中使用)
- (void)viewDidLoad { [super viewDidLoad]; // 1.1 创建按钮对象 // UIButton *button = [[UIButton alloc] ini ...