Given an array of distinct integers candidates
and a target integer target
, return a list of all unique combinations of candidates
where the chosen numbers sum to target
. You may return the combinations in any order.
The same number may be chosen from candidates
an unlimited number of times. Two combinations are unique if the of at least one of the chosen numbers is different.
The test cases are generated such that the number of unique combinations that sum up to target
is less than 150
combinations for the given input.
Example 1:
Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3],[7]] Explanation: 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 7 is a candidate, and 7 = 7. These are the only two combinations.
Example 2:
Input: candidates = [2,3,5], target = 8 Output: [[2,2,2,2],[2,3,3],[3,5]]
Example 3:
Input: candidates = [2], target = 1 Output: []
Constraints:
1 <= candidates.length <= 30
2 <= candidates[i] <= 40
- All elements of
candidates
are distinct. 1 <= target <= 40
A:
递归
Mistakes:
1: 利用递归调用,基础条件检验的时候,忘了检测 startIndex是否越界。
2:肏, 忘了最基本的, 当条件符合的时候,就加上,并且返回啊。 检查target 是否和第一个数字吻合。
Learned:
----------------------------------第二遍-----递归的时候, 把所有的index after beginIndex 都用for loop遍历了一遍,造成了,极大的重复-----------------
----------------------第三遍也是 同样的错误, 哎~~~~~~~~~~~~~~~
递归
class Solution {public:vector<vector<int>> combinationSum(vector<int>& candidates, int target) {sort(candidates.begin(), candidates.end());vector<vector<int>> res;vector<int> curV;dfs(candidates, 0, res, curV, target);return res;}private:void dfs(vector<int>& candidates, int start,vector<vector<int>>& res, vector<int>& curV, int target ){int n = candidates.size();if(target<0){return;}else if(target == 0){res.push_back(curV);}else if(start >= n || candidates[start] > target){return;}else{// two options, not use,dfs(candidates, start + 1, res, curV, target);// usecurV.push_back(candidates[start]);dfs(candidates, start, res, curV, target - candidates[start]);curV.pop_back();}}};
Mistakes:
1: 利用递归调用,基础条件检验的时候,忘了检测 startIndex是否越界。
2:肏, 忘了最基本的, 当条件符合的时候,就加上,并且返回啊。 检查target 是否和第一个数字吻合。
Learned:
----------------------------------第二遍-----递归的时候, 把所有的index after beginIndex 都用for loop遍历了一遍,造成了,极大的重复-----------------
----------------------第三遍也是 同样的错误, 哎~~~~~~~~~~~~~~~
public class Solution { public ArrayList<ArrayList<Integer>> combinationSum(int[] A, int target) { Arrays.sort(A); return helper(A, 0, target); } private ArrayList<ArrayList<Integer>> helper(int[] A, int start, int target) { ArrayList<ArrayList<Integer>> all = new ArrayList<ArrayList<Integer>>(); if (start >= A.length || target < A[start]) return all; if (target == A[start]) { ArrayList<Integer> al = new ArrayList<Integer>(); al.add(target); all.add(al); return all; } // target > A[start], we can continue to divide it // use A[i] ArrayList<ArrayList<Integer>> less1 = helper(A, start, target - A[start]); for (int j = 0; j < less1.size(); j++) less1.get(j).add(0, A[start]); all.addAll(less1); // do not use A[i]; less1 = helper(A, start + 1, target); all.addAll(less1); return all; } }
No comments:
Post a Comment