LeetCode - Combination Sum I && II
来源:互联网 发布:百度竞价调价软件 编辑:程序博客网 时间:2024/04/29 03:29
Combination Sum I
https://leetcode.com/problems/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.
- 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]
这种要求返回所有的题都可以用递归。
这里要注意的有3点:
1. 要求不能有重复,由于每层递归对应每个位置,因此当前位置(当前层的递归)上,不能用同一个数,因此遇到相同的数要略过,因此首先要排序,然后见for循环中的while循环。
2. 重复情况还有一种是[1,3], [3,1]这种,虽然每个位置数不一样,但本质其实是一样的,要避免这种情况,是在helper函数中加start变量,保证后面位置只用当前位置用的数的后面的数。
3. 解中的数要求是递增的。因此对数组排序。
代码如下:
public class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> rst = new LinkedList<List<Integer>>(); if(candidates==null || candidates.length==0) return rst; Arrays.sort(candidates); //排序,保证递增以及避免重复 List<Integer> solution = new LinkedList<Integer>(); helper(candidates, rst, solution, target, 0); return rst; } public void helper(int[] candidates, List<List<Integer>> rst, List<Integer> solution, int target, int start){ if(target==0){ rst.add(new LinkedList<Integer>(solution)); return; } if(target<0) return; for(int i=start; i<candidates.length; i++){ //i从start开始,当前位置只能使用之前没有看过的数 solution.add(candidates[i]); helper(candidates, rst, solution, target-candidates[i], i); solution.remove(solution.size()-1); while(i<candidates.length-1 && candidates[i+1]==candidates[i]) i++; //为当前位置略过重复数 } }}
时间复杂度和空间复杂度都是exponential的。
Combination Sum II
https://leetcode.com/problems/combination-sum-ii/
这道题跟上一道题基本一模一样,唯一区别就是每个数只能用一次,因此代码上只需要改一点点就行,即下一层递归不能再从当前数开始,而要从下一个数开始了。
代码如下:
public class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer>> rst = new LinkedList<List<Integer>>(); if(candidates==null || candidates.length==0) return rst; Arrays.sort(candidates); List<Integer> solution = new LinkedList<Integer>(); helper(candidates, rst, solution, target, 0); return rst; } public void helper(int[] candidates, List<List<Integer>> rst, List<Integer> solution, int target, int start){ if(target==0){ rst.add(new LinkedList<Integer>(solution)); return; } if(target<0) return; for(int i=start; i<candidates.length; i++){ solution.add(candidates[i]); helper(candidates, rst, solution, target-candidates[i], i+1); //跟上一题唯一不同的就是,上一题最后一个参数是i,这里是i+1 solution.remove(solution.size()-1); while(i<candidates.length-1 && candidates[i+1]==candidates[i]) i++; } }}
- Leetcode Combination Sum I & II
- 【LeetCode】Combination Sum I && II
- leetcode Combination Sum I &II
- LeetCode - Combination Sum I && II
- Combination Sum I/II - Leetcode
- leetcode:Combination Sum(I,II)
- LeetCode | Combination Sum I,II
- LeetCode题解:Combination Sum I and II
- LeetCode Solutions : Combination Sum I & II
- 【LeetCode】Combination Sum I & II 解题报告
- [leetcode] 39& 40 Combination Sum I& II
- [LeetCode 39&40] Combination Sum I & II
- [leetcode题后感]combination sum i,ii
- LeetCode 39/40. Combination Sum i, ii
- leetcode Combination Sum I/II/III/IV
- [LeetCode OJ]Combination Sum I && II
- Combination Sum I, II
- LeetCode: Combination Sum II
- LeetCode - Combination Sum
- 软件重构
- 演示学习Qualcomm Vuforia 视频
- Majority Element
- 学习Stack,并实现从尾到头打印链表LinkedList
- LeetCode - Combination Sum I && II
- sorting - merge sort
- LeetCode - Best Time to Buy and Sell Stock I && II && III && IV
- 开篇写给自己
- Android 开发之 emoji 表情
- scala learning notebook1
- 【控件篇】AutoCompleteTextView 简单实用案例代码
- 黑马程序员 — Objective-C学习笔记(二):继承
- osg 学习 2 UpdateCallback, EventHandler, 运动的相机