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> &num, int target) { vector<vector<int> > ret; if(target <= 0) return ret; if(num.size() == 0) return ret; sort(num.begin(), num.end()); vector<int> tmp; iter(num, target, 0, tmp, ret); return ret; } void iter(vector<int> &num, int target, int beg, vector<int> tmp, vector<vector<int> > &ret) { if(target == 0) { ret.push_back(tmp); return; } if(target > 0) { for(int i = beg; i < num.size(); ++i) { if(i == beg || num[i] != num[i-1]) { tmp.push_back(num[i]); iter(num, target-num[i], i+1, tmp, ret); tmp.pop_back(); } } } } };
欢迎关注微信公众号——计算机视觉
相关推荐
239 Combination Sum II 579 240 Combination Sum III 581 241 Combinations 583 242 Letter Combinations of a Phone Number 587 243 Restore IP Addresses 589 244 Reverse Integer 591 245 Palindrome Number 593
Combination Sum II Combination Sum III Generate Parentheses Sudoku Solver Word Search 总结 分治法 Pow(x,n) Sqrt(x) 贪心法 Jump Game Jump Game II Best Time to Buy and Sell Stock Best Time to Buy and ...
Combination Sum Medium 回溯 0040 Combination Sum II Medium 回溯 0046 Permutations Medium 回溯 0047 Permutations II Medium 递归、回溯 0051 N-Queens Hard 回溯 0053 Maximum Subarray Easy 动态规划 0069 ...
syntax of C++ in combination with its applications to current topics of interest. In particular, we develop libraries, frameworks and applications for a variety of derivatives models using numerical ...
排列组合01法的Java实现,实现基于字典排序的结果输出
leetcode题库 Little Algorithm 从 2020 年初开始,我在公众号《面向大象编程》上发表面试算法、...Combination Sum组合总和 Combination Sum II组合总和 II Permutations全排列 Permutations II全排列 II Maximum Suba
This is all the code you need (in combination with Win32++) to create and display a simple window. Note that in order to add the Win32++ code to our program, we use an #include statement as shown ...
039:Combination Sum 040:Combination Sum II 046:Permutations 047:Permutations II 051:N-Queens 052:N-Queens II 071: Letter Combinations of a Phone Number 093:Restore IP Addresses 树的遍历问题也...
leetcode打不开Leetcode Note Tips Tip1: Two pointer ...Combination Sum II) Tip5: 鸽笼原理要记得,如果题目说要constant extra space,八成就是用input array + swap(#41. First Missing Positive
一个C++学生管理系统 一个C++学生管理系统 一个C++学生管理系统
Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler. Dev-C++ is a Free Software distributed under the terms of the GNU General Public License (GPL) Dev-C++ features ...
JavaFX+Jfoenix 学习笔记系列文章JavaFX+Jfoenix 学习笔记(九)--KeyCombination快捷键源码
• Examples : How can it be used well by itself and in combination with other features? What are the key techniques and idioms? What are the implications for maintainability and per- formance? Part I...
a program for find combination of N Number to reach a Sum. this solve sudoku puzzle.
python实现基本算法 所有组合 所有排列 所有子序列 ... Combination Sum Hamiltonian Cycle Knight Tour Minimax Minmax N Queens N Queens Math Rat In Maze Sudoku Sum Of Subsets Word Search
This paper describes how X-FEM coupled with level set methods can be used to solve complex three-dimensional industrial fracture mechanics problems through combination of an object-oriented (C++) ...
The intelligent surfer probabilistic combination of link and content information in pagerank
html5和c++开源 K线图工具, Create interactive charts easily for your web projects. Used by tens of thousands of developers and 61 out of the world's 100 largest companies, Highcharts is the simplest...
combinationSum ( self , candidates , target ): def backtrack ( tmp , start , end , target ): if target == 0 : ans . append ( tmp [:]) elif target > 0 : for i in range ( start , end ): tmp . append ( ...