DFS Combinations

思想:

方法一:DFS

start标记本次深度的递归从哪个数开始;

cur标记到了第几层;

path记录递归路径;

c++ code:

class Solution {
public:
    //Combinations : dfs
    void dfs(int n,int k,int start,int cur,vector<int> &path,vector<vector<int>> &result) {
        if(cur == k) {
            result.push_back(path);
        }
        for(int i=start; i<=n; ++i) {
            path.push_back(i);
            dfs(n,k,i+1,cur+1,path,result);
            path.pop_back();
        }
    }
    vector<vector<int> > combine(int n, int k) {
        vector<vector<int>> result;
        vector<int> path;
        dfs(n,k,1,0,path,result);
        return result;
    }
};



java code:

public class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        List<Integer> path = new ArrayList<Integer>();
        
        dfs(res, path, 1, n ,k);
        
        return res;
    }
    
    public void dfs(List<List<Integer>> res, List<Integer> path, int start, int n, int k) {
        if(k == 0) {
            res.add(new ArrayList(path));
            return;
        }
        
        for(int i = start; i <= n; ++i) {
            path.add(i);
            dfs(res, path, i + 1, n, k - 1);
            path.remove(path.size() - 1);
        }
    }
}


方法二:迭代


iota模板函数:

template <class ForwardIterator, class T>
  void iota (ForwardIterator first, ForwardIterator last, T val);
Store increasing sequence
Assigns to every element in the range  [first,last) successive values of  val, as if incremented with  ++val after each element is written.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
template <class ForwardIterator, class T>
  void iota (ForwardIterator first, ForwardIterator last, T val)
{
  while (first!=last) {
    *first = val;
    ++first;
    ++val;
  }
}

需要包含头文件#include <numeric>;


fill_n模板函数:

std::fill_n

template <class OutputIterator, class Size, class T>
  OutputIterator fill_n (OutputIterator first, Size n, const T& val);
Fill sequence with value
Assigns  val to the first  n elements of the sequence pointed by  first.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
template <class OutputIterator, class Size, class T>
  OutputIterator fill_n (OutputIterator first, Size n, const T& val)
{
  while (n>0) {
    *first = val;
    ++first; --n;
  }
  return first;     // since C++11
}

需要包含头文件#include <algorithm>;


class Solution {
public:
    //Combinations : iteration
    vector<vector<int> > combine(int n, int k) {
        vector<int> values(n);
        iota(values.begin(),values.end(),1);
        vector<bool> select(n,false);
        fill_n(select.begin(), k, true);
        vector<vector<int>> res;
        do {
            vector<int> one(k);
            for(int i=0,index=0; i<n; i++) {
                if(select[i]) {
                    one[index++] = values[i];
                }
            }
            res.push_back(one);
        }while(prev_permutation(select.begin(), select.end()));
        return res;
    }
};

or

注意next_permutation()和prev_permutation的区别:

bool数组的字典序是false<true;

因此如果使用prev_permutation(),刚开始就要到全排列的最后一种情况,即前面全是true,后面全是false;

如果使用next_permutation(),则相反,刚开始是全排列的第一种情况,即前面全是false,后面全是true。

class Solution {
public:
    //Combinations : iteration
    vector<vector<int> > combine(int n, int k) {
        vector<int> values(n);
        iota(values.begin(),values.end(),1);
        vector<bool> select(n,true);
        fill_n(select.begin(), k, false);
        vector<vector<int>> res;
        do {
            vector<int> one(k);
            for(int i=0,index=0; i<n; i++) {
                if(select[i]==false) {
                    one[index++] = values[i];
                }
            }
            res.push_back(one);
        }while(next_permutation(select.begin(), select.end()));
        return res;
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值