leetcode 40. Combination Sum II DFS深度优先搜索

来源:互联网 发布:网狐6603棋牌源码 编辑:程序博客网 时间:2024/06/05 14:48

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

和上一道题不一样的是这里不允许重复,稍作修改即可。

代码如下:

import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class Solution {    public List<List<Integer>> combinationSum2(int[] candidates, int target)    {        List<List<Integer>> res=new ArrayList<List<Integer>>();        List<Integer> one=new ArrayList<>();        //考虑特殊情况        if(candidates==null || candidates.length<0)            return res;        else        {            Arrays.sort(candidates);            getSum(candidates,target,res,one,0,0);            return res;        }    }    private void getSum(int[] candidates, int target, List<List<Integer>> res,            List<Integer> one, int sum,int level)     {        //前面两个if的判断不能互换位置,否者会报错        if(sum==target)            res.add(new ArrayList<>(one));        else if(sum>target || level==candidates.length)            return ;        else        {            //题目中说可以重复的使用某一个数,所以level可以不变            for(int i=level;i<candidates.length;i++)            {                //这个过程就是剪枝,在递归之处剪枝                if(level!=i && candidates[i]==candidates[i-1])                    continue;                one.add(candidates[i]);                //所以这里的level为i+1就是表示递归到下一层,但是得到的解可能重复                getSum(candidates, target, res, one, sum+candidates[i], i+1);                one.remove(one.size()-1);            }        }    }}

下面是C++的做法,和上一道题一样,这是一道很经典的DFS深度优先遍历的做法,这道题十分需要注意的是递归的前面两个if的位置不可以颠倒,要不然或报错

代码如下:

#include <iostream>#include <vector>#include <algorithm>using namespace std;class Solution {public:    vector<vector<int>> res;    vector<vector<int>> combinationSum2(vector<int>& candidates, int target)     {        if (candidates.size() <= 0)            return res;        sort(candidates.begin(),candidates.end());        vector<int> one;        getAllByDFS(candidates,target,one,0,0);        return res;    }    void getAllByDFS(vector<int>& can, int target, vector<int>& one, int sum, int level)    {           if (sum == target)        {            //C++中可以这么写,因为one是使用的引用参数,但是one添加到res的过程中会做复制操作            //所以直接添加one也是可以的,但是在Java中就不可以了            res.push_back(one);            return;        }else if (sum > target || level == can.size())            return;        else        {            for (int i = level; i < can.size(); i++)            {                if (i != level && can[i] == can[i - 1])                    continue;                one.push_back(can[i]);                getAllByDFS(can, target, one, sum + can[i], i+1);                one.erase(one.end()-1);            }        }    }};//1 1 2 5 6 7 10
原创粉丝点击