Leetcode-Combination Sum(39,40,216)

Combination Sum(39,40,216)

Given a set of candidate numbers (C) (without duplicates) 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]
]

Subscribe to see which companies asked this question.

public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(candidates);
        backtrack(list, new ArrayList<>(), candidates, target, 0);
        return list; 
    }
    
    private void backtrack(List<List<Integer>> list,List<Integer> tmpList,int[] nums, int remain, int start ){
        if(remain < 0) return;
        else if(remain == 0) list.add(new ArrayList<>(tmpList));
        else{
           for(int i=start;i<nums.length;i++){
               tmpList.add(nums[i]);
               backtrack(list, tmpList, nums, remain-nums[i], i);// not i + 1 because we can reuse same elements
               tmpList.remove(tmpList.size()-1);
            
            } 
        }
        
    }
    
}

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]
]

Subscribe to see which companies asked this question.

public class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(candidates);
        backtrack(list, new ArrayList<>(), candidates, target, 0);
        return list;
    }

    private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){
        if(remain < 0) return;
        else if(remain == 0) list.add(new ArrayList<>(tempList));
        else{
            for(int i = start; i < nums.length; i++){
                if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates
                tempList.add(nums[i]);
                backtrack(list, tempList, nums, remain - nums[i], i + 1);
                tempList.remove(tempList.size() - 1); 
            }
        }
    } 
}

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]]

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

Show Tags

Show Similar Problems

public class Solution {
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> list = new ArrayList<>();
        //Arrays.sort(); arrays=[1,2,3,4,5,6,7,8,9]
        backtrack(list,new ArrayList<Integer>(),k,1,n);
        return list;
    }
    
    private void backtrack(List<List<Integer>> list,List<Integer> tmpList,int k,int start,int n){
        if(tmpList.size()==k &&n==0){
            List<Integer> li = new ArrayList<Integer>(tmpList);
            list.add(li);
            return;
        }
        for(int i=start;i<=9;i++){
            tmpList.add(i);
            backtrack(list,tmpList,k,i+1,n-i);
            tmpList.remove(tmpList.size()-1);
        }
    }
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容