[Algorithm] Combination问题

Combination

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

Example

For example,
If n = 4 and k = 2, a solution is:
[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4]]

例如:n=4,k=2

过程:i=1这层

          start=1,for循环,添加1到list,递归helper,

          start=2,判断list.size()不符合要求,for循环,添加2到list,递归helper

          start=3,判断list.size()符合要求,result添加list,return;

          到start=2这层,删除list里的最后元素2,for循环i++,i=3,添加3到list,递归helper

public class Solution {
    /**
     * @param n: Given the range of numbers
     * @param k: Given the numbers of combinations
     * @return: All the combinations of k numbers out of 1..n
     */
    public List<List<Integer>> combine(int n, int k) {
		List<List<Integer>> result=new ArrayList<>();
		if(n<0 || k>n){
		   return result; 
		}
		helper(result,new ArrayList(),1,n,k);
		return result;
    }
    
    private void helper(List<List<Integer>> result,List<Integer> list,int start,int n,int k){
        if(list.size()==k){
            result.add(new ArrayList<>(list));
            return;
        }
        
        for(int i=start;i<=n;i++){
            list.add(i);
            helper(result,list,i+1,n,k);
            list.remove(list.size()-1);
        }
    }
}

Combination Sum

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

public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> result=new ArrayList<>();
        List<Integer> list=new ArrayList<>();
        if(candidates==null || candidates.length==0){
            return result;
        }
        int[] nums=removeDuplicates(candidates);
        helper(nums,target,0,0,result,list);
        return result;
    }
    
    private void helper(int[] nums,int target,int startIndex,int sum,List<List<Integer>> result,List<Integer> list){
        if(sum==target){
            result.add(new ArrayList<Integer>(list));
            return;
        }
        
        for(int i=startIndex;i<nums.length;i++){
            if(sum<target){
                list.add(nums[i]);
                helper(nums,target,i,sum+nums[i],result,list);
                list.remove(list.size()-1);
            }
        }
    }
    
    private int[] removeDuplicates(int[] nums){
        Arrays.sort(nums);
        int index=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]!=nums[index]){
                index++;
                nums[index]=nums[i];
            }
        }
        int[] result=new int[index+1];
        for(int i=0;i<result.length;i++){
            result[i]=nums[i];
        }
        return result;
    }
}
Combination Sum II

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

public class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> result=new ArrayList<>();
        List<Integer> list=new ArrayList<>();
        if(candidates==null || candidates.length==0){
            return result;
        }
        Arrays.sort(candidates);
        helper(candidates,target,0,0,result,list);
        return result;
    }
    
    private void helper(int[] nums,int target,int startIndex,int sum,List<List<Integer>> result,List<Integer> list){
        if(sum==target){
            result.add(new ArrayList<Integer>(list));
            return;
        }
        
        for(int i=startIndex;i<nums.length;i++){
            if(sum<target){
                if(i!=0 && nums[i]==nums[i-1] && i!=startIndex){      //和前面元素值相同,就不取了,前面已取过
                    continue;     
                } 
                list.add(nums[i]);
                helper(nums,target,i+1,sum+nums[i],result,list);
                list.remove(list.size()-1);
            }
        }
    }  
}
Combination Sum III

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

public class Solution {                                              //一刷
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> result=new ArrayList<>();
        List<Integer> list=new ArrayList<>();
        if(k<=0){
            return result;
        }
        
        int[] nums=new int[9];
        nums[0]=1;
        for(int i=1;i<9;i++){
            nums[i]=nums[i-1]+1;
        }
        helper(nums,n,k,0,0,result,list);
        return result;
    }
    
    private void helper(int[] nums,int n,int k,int startIndex,int sum,List<List<Integer>> result,List<Integer> list){
        if(sum==n){
            if(list.size()==k){                                      //对size的判断
                result.add(new ArrayList<Integer>(list));      
                return;
            }   
        }
        for(int i=startIndex;i<nums.length;i++){
            if(sum<n){
                list.add(nums[i]);
                helper(nums,n,k,i+1,sum+nums[i],result,list);
                list.remove(list.size()-1);
            }
        }   
    }
}

public class Solution {                                              //二刷写法
    public List<List<Integer>> combinationSum3(int k, int n) {              
        List<List<Integer>> result=new ArrayList<>();
        if(n<0 || k>n){
            return result;
        }
        helper(result,new ArrayList<>(),k,0,n,1);
        return result;
    }
    
    private void helper(List<List<Integer>> result,List<Integer> list,int k, int sum,int n,int start){
        if(sum==n && list.size()==k) {
            result.add(new ArrayList<>(list));
            return;
        }
        
        for(int i=start;i<=9;i++){
            if(sum<n && list.size()<k){
                list.add(i);
                helper(result,list,k,sum+i,n,i+1);
                list.remove(list.size()-1);
            }
        }        
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值