LeetCode-39&40&216.Combination Sum

39、

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

解题思路:

先排好序,然后每次递归中把剩下的元素一一加到结果集合中,并且把目标减去加入的元素,然后把剩下元素(包括当前加入的元素)放到下一层递归中解决子问题。算法复杂度因为是NP问题,所以是指数量级的。

代码中for循环中有一个判断,是为了去除重复元素产生重复结果的影响,因为数可以重复使用,所以重复的元素也就没有作用了

public class Solution
{
    public IList<IList<int>> CombinationSum(int[] candidates, int target)
        {
            Array.Sort(candidates);
            IList<IList<int>> res =new List<IList<int>>();
            IList<int> combination=new List<int>();
            Fun(candidates, target, res, combination, 0);
            return res;
        }

        private void Fun(int[] candidates, int target, IList<IList<int>> res, IList<int> combination, int begin)
        {
            if (target==0)
            {
                res.Add(new List<int>(combination));
                return;
            }
            for (int i = begin; i != candidates.Length && target >= candidates[i]; i++)
            {
                if (i > 0 && candidates[i] == candidates[i - 1])
                    continue;
                combination.Add(candidates[i]);
                Fun(candidates, target - candidates[i], res, combination, i);
                combination.RemoveAt(combination.Count-1);
            }
        }
}

40、元素不可重复使用
在39的代码上稍作修改即可,注意递归的begin值必须+1
public class Solution 
{
        public IList<IList<int>> CombinationSum2(int[] candidates, int target)
        {
            Array.Sort(candidates);
            IList<IList<int>> res =new List<IList<int>>();
            IList<int> combination=new List<int>();
            Fun(candidates, target, res, combination, 0);
            return res;
        }

        private void Fun(int[] candidates, int target, IList<IList<int>> res, IList<int> combination, int begin)
        {
            if (target==0)
            {
                res.Add(new List<int>(combination));
                return;
            }
            if (begin >= candidates.Length)
                return;
            for (int i = begin; i < candidates.Length && target >= candidates[i]; i++)
            {
                if (i > begin && candidates[i] == candidates[i - 1])
                    continue;
                combination.Add(candidates[i]);
                Fun(candidates, target - candidates[i], res, combination, i+1);
                combination.RemoveAt(combination.Count-1);
            }
        }
}

216、

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.

Ensure that numbers within the set are sorted in ascending order.

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 IList<IList<int>> CombinationSum3(int k, int n)
        {
            int[] nums = new int[] {1,2,3,4,5,6,7,8,9};
            IList<IList<int>> res =new List<IList<int>>();
            IList<int> list=new List<int>();
            Fun(nums, n, res, list, 0,k);
            return res;
        }

        private void Fun(int[] nums, int n, IList<IList<int>> res, IList<int> list, int begin,int k)
        {
            if (n == 0 && k==0)
            {
                res.Add(new List<int>(list));
                return;
            }
            if (begin >= 9 || k<0)
                return;
            for (int i = begin; i < 9 && n >= nums[i]; i++)
            {
                list.Add(nums[i]);
                Fun(nums, n - nums[i], res, list, i + 1,k-1);
                list.RemoveAt(list.Count - 1);
            }
        }
}

优化:
其实可以不需要那个nums数组,改动的地方有点多,但思路基本不变

public class Solution 
{
        public IList<IList<int>> CombinationSum3(int k, int n)
        {
            IList<IList<int>> res =new List<IList<int>>();
            IList<int> list=new List<int>();
            Fun(n, res, list,1,k);
            return res;
        }

        private void Fun(int n, IList<IList<int>> res, IList<int> list, int begin,int k)
        {
           if (n == 0 && k==0)
            {
                res.Add(new List<int>(list));
                return;
            }
            if (begin >= 10|| k<0)
                return;
            for (int i = begin; i < 10 ; i++)
            {
                list.Add(i);
                Fun(n - i, res, list, i + 1,k-1);
                list.RemoveAt(list.Count - 1);
            }
        }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值