Combination Sum leetcode java

题目:

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, a1a2 ≤ … ≤ 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]

题解

还是老问题,用DFS找解决方案,不同点是,这道题: The same repeated number may be chosen from C unlimited number of times.

所以,每次跳进递归不用都往后挪一个,还可以利用当前的元素尝试。

同时,这道题还要判断重复解。用我之前介绍的两个方法:

1.       if(i>0 && candidates[i] == candidates[i-1])//deal with dupicate
                 continue;

2.       if(!res.contains(item))
                res.add(new ArrayList<Integer>(item));

这两个方法解决。

代码如下:

1     public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {  
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();  
 3         ArrayList<Integer> item = new ArrayList<Integer>();
 4         if(candidates == null || candidates.length==0)  
 5             return res; 
 6             
 7         Arrays.sort(candidates);  
 8         helper(candidates,target, 0, item ,res);  
 9         return res;  
10     }  
11     
12     private void helper(int[] candidates, int target, int start, ArrayList<Integer> item,   
13     ArrayList<ArrayList<Integer>> res){  
14         if(target<0)  
15             return;  
16         if(target==0){  
17             res.add(new ArrayList<Integer>(item));  
18             return;  
19         }
20         
21         for(int i=start;i<candidates.length;i++){  
22             if(i>0 && candidates[i] == candidates[i-1])//deal with dupicate
23                 continue;  
24             item.add(candidates[i]);
25             int newtarget = target - candidates[i];
26             helper(candidates,newtarget,i,item,res);//之所以不传i+1的原因是:
27                                                     //The same repeated number may be 
28                                                     //chosen from C unlimited number of times.
29             item.remove(item.size()-1);  
30         }  
31     }

Combination Sum leetcode java

时间: 2024-08-08 09:47:06

Combination Sum leetcode java的相关文章

Path Sum leetcode java

题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true,

4 Sum leetcode java

题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending or

3 Sum leetcode java

题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s

Binary Tree Maximum Path Sum leetcode java

题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. 题解: 递归求解. 取当前点和左右边加和,当前点的值中最大的作为本层返回值.并在全局维护一个max.使用数组,因为是引用类型.所以在递归过程中可以保存结果. 代码如下: 1

[LeetCode] 40. Combination Sum II Java

题目: 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

leetcode 40 Combination Sum II --- java ----100%

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 posi

216. Combination Sum III java solutions

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n

Combination Sum —— LeetCode

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

Two Sum Leetcode Java

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2