leetcode题目 寻找和为SUM的集合系列问题
来源:互联网 发布:淘宝小二删除中差评 编辑:程序博客网 时间:2024/06/01 16:21
题目一: Given a set of candidate numbers (C) 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.
•Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
•The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
思路: 递归解决,但是从最后提交的结果来看效率并不高,或许还有更好的算法?
代码:
class Solution {public: vector<vector<int>> combinationSum(vector<int>& candidates, int target) { sort(candidates.begin(),candidates.end()); vector<vector<int>> result; vector<int> temp; for(auto i=candidates.begin();i!=candidates.end();++i) { temp.push_back(*i); FindSumSet(i,candidates.end(),result,temp,target-*i); temp.clear(); } return result; } void FindSumSet(vector<int>::iterator begin,vector<int>::iterator end,vector<vector<int>>& result,vector<int> temp ,int target) { if(target==0) { result.push_back(temp); return; } else if(target<0) return; else { auto copy_temp=temp; for(auto i=begin;i!=end;++i) { copy_temp=temp; copy_temp.push_back(*i); FindSumSet(i,end,result,copy_temp,target-*i); } } return; }};
测试结果: 虽然通过了,但是时间效率很差,只击败了6.17%的代码
题目二: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
•All numbers (including target) will be positive integers.
•Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
•The solution set must not contain duplicate combinations.
For example, given candidate set 10,1,2,7,6,1,5 and target 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
思路: 只是比第一题复杂一点点,把第一题的代码改改就可以,但是也继承了第一题时间效率差的问题。
class Solution {public: vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { sort(candidates.begin(),candidates.end()); vector<vector<int>> result; vector<int> temp; for(auto i=candidates.begin(),j=i;i!=candidates.end();i=j) { temp.push_back(*i); FindSumSet(i,candidates.end(),result,temp,target-*i); temp.clear(); while(++j!=candidates.end()&&*j==*i); } return result; } void FindSumSet(vector<int>::iterator begin,vector<int>::iterator end,vector<vector<int>>& result,vector<int> temp ,int target) { if(target==0) { result.push_back(temp); return; } else if(target<0) return; else { auto copy_temp=temp; for(auto i=begin+1,j=i;i!=end;i=j) { copy_temp=temp; copy_temp.push_back(*i); FindSumSet(i,end,result,copy_temp,target-*i); while(++j!=end&*j==*i); } } return; }};
测试结果: 虽然通过了,但是时间效率很差,只击败了10.71%的代码
- leetcode题目 寻找和为SUM的集合系列问题
- leetcode题目 寻找和为SUM的两数(O(nlogn)和O(n)解法)
- 1048. Find Coins (25) -- 二分法,寻找和为定值的两个数 (Two Sum - leetcode)
- 【LeetCode】Two Sum && 【九度】题目1352:和为S的两个数字
- 数组中寻找和为sum的两个元素
- 寻找和为Sum的多个数-1
- 寻找和为Sum的多个数-2
- LeetCode 3Sum 三个数和为零的集合 C++完整程序
- [LeetCode]-Combination Sum I&II 求相加和为target的集合
- 集合中寻找和为某值的子集
- LeetCode 之 two Sum寻找两个相加之和为给定值的两个数
- leetcode之深搜递归回溯类-----1/167/653. two sum(记忆化搜索寻找和为给定值的两个数)
- 一个集合所有子集和为sum的组合打印
- leetcode系列(24)Path Sum 判断二叉树中和为Sum的路径是否存在
- Two sum 在数组中寻找和为指定值的两个数
- LeetCode: 关于Sum的题目合集
- [leetcode] 回溯法 Combination Sum 系列问题
- leetcode题目 反转链表系列问题
- java关键字 instanceOf
- java写入MySQL乱码或?解决
- python 网络编程(一):socket 模块
- C#学习日记15----引用类型 之 string类型用法总结
- 6.Swift 触摸实现缩放
- leetcode题目 寻找和为SUM的集合系列问题
- IO流
- 深入 HBase 架构解析(1)
- C++源代码生成可执行文件的四个步骤
- 【笔试】54、不用加减乘除做加法
- 数列有序!
- 深入 HBase 架构解析(2)
- 关于ios9中得ALAssetsLibrary框架过时问题
- 一元多项式的乘法与加法运算【Java实现--通过单项链表实现队列】