首页 > 代码库 > Combination 题目整理
Combination 题目整理
Combination Sum
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.
- 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]]
用for循环+ 递归的方式求解
for 循环套在外层,表示遍历数组的第i个数字
内层递归表示结果list里的第i个数字(比如第一层递归是寻找list里的第一个数字)
![技术分享](/d/file/p/2024/07/08/5b28f7a0b3ec0b2c803cccc5aa2df541.gif)
1 public class Solution { 2 public List<List<Integer>> combinationSum(int[] candidates, int target) { 3 int size = candidates.length; 4 List<List<Integer>> res = new ArrayList<List<Integer>>(); 5 List<Integer> list = new ArrayList<Integer>(); 6 helper (res, list, candidates, target, 0); 7 return res; 8 } 9 private static void helper(List<List<Integer>> res,List<Integer> list, int[] candidates, int target, int pos) {10 if (target == 0) {11 res.add(new ArrayList<Integer>(list));12 return;13 14 } 15 16 if (target < 0) {17 return;18 }19 int prev = -1;20 for (int i = pos; i < candidates.length; i++) {21 if(prev == candidates[i]) {22 continue;23 }24 list.add(candidates[i]);25 helper(res, list, candidates, target - candidates[i], i);26 prev = candidates[i];27 list.remove(list.size() - 1);28 }29 }30 }
---------------我是分割线--------------------------------------
Combination Sum 2
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.
- 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]]
这个题目要注意下面的错误情况
用递归寻找第一个元素时,如果之前的结果里已经把1当作第一个元素,然后pop出去了,那么下次的1再想做为第一个元素的话就直接continue,这样就避免了2个[1,2,5]的情况,也就是说,在for循环里面不允许有相同的元素!只有第一次出现才做为valid
![技术分享](/d/file/p/2024/07/08/5b28f7a0b3ec0b2c803cccc5aa2df541.gif)
1 public class Solution { 2 public List<List<Integer>> combinationSum2(int[] candidates, int target) { 3 int size = candidates.length; 4 Arrays.sort(candidates); 5 List<List<Integer>> res = new ArrayList<List<Integer>>(); 6 List<Integer> list = new ArrayList<Integer>(); 7 helper (res, list, candidates, target, 0); 8 return res; 9 }10 private static void helper(List<List<Integer>> res,List<Integer> list, int[] candidates, int target, int pos) {11 if (target == 0) {12 res.add(new ArrayList<Integer>(list));13 return;14 15 } 16 if (target < 0) {17 return;18 }19 int prev = -1;20 for (int i = pos; i < candidates.length; i++) {21 if (candidates[i] > target) {22 break;23 }24 25 if (prev != -1 && prev == candidates[i]) {26 continue;27 }28 29 if (candidates[i] != -1) {30 list.add(candidates[i]);31 helper(res, list, candidates, target - candidates[i], i + 1);32 prev = candidates[i];33 list.remove(list.size() - 1);34 }35 }36 }37 }
---------------我是分割线--------------------------------------
Combination Sum 3
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 = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]
![技术分享](/d/file/p/2024/07/08/5b28f7a0b3ec0b2c803cccc5aa2df541.gif)
1 public class Solution { 2 public List<List<Integer>> combinationSum3(int k, int n) { 3 List<List<Integer>> res = new ArrayList<List<Integer>>(); 4 List<Integer> list = new ArrayList<Integer>(); 5 helper (res, list, k, n, 1); 6 return res; 7 } 8 private static void helper(List<List<Integer>> res,List<Integer> list, int k, int n, int current) { 9 if (n == 0 && list.size() == k) {10 res.add(new ArrayList<Integer>(list));11 return;12 13 } 14 if (list.size() == k || n < 0) {15 return;16 }17 18 19 for (int i = current; i < 10; i++) {20 list.add(i);21 helper(res, list, k, n - i, i + 1);22 list.remove(list.size() - 1);23 }24 }25 }
相当于[1,2,3,4,5,6,7,8,9] 的数组,然后增加了k的控制条件
---------------我是分割线--------------------------------------
Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
nums = [1, 2, 3]target = 4The possible combination ways are:(1, 1, 1, 1)(1, 1, 2)(1, 2, 1)(1, 3)(2, 1, 1)(2, 2)(3, 1)Note that different sequences are counted as different combinations.Therefore the output is 7.
Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?
巨坑,只需要返回结果个数,那么其实是个动态规划的题目,类似于爬楼梯!!!
Combination 题目整理