LeetCode Combinations Problem using backtracing and DFS

来源:互联网 发布:知已是什么意思呀 编辑:程序博客网 时间:2024/05/18 09:17

给出两个整数n和k,返回从1……n中选出的k个数的组合。

样例
例如 n = 4 且 k = 2
返回的解为:

[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4]]

https://leetcode.com/problems/combinations/
http://www.lintcode.com/zh-cn/problem/combinations/

以下是我用C写的,但是效率太低,会报

Time Limited Exceeded!

#include <iostream>#include <cstdio>#define N 1000bool used[N];int ans[N];int k;int n;using namespace std;void dfs(int key){    if (key == k)    {        for (int i = 0; i < k - 1; ++i)            if (ans[i] > ans[i+1])                return;        printf(",[");        for (int i = 0; i < k; ++i)            printf("%d ",ans[i]);        printf("]");    }    for (int i = 0; i < n; ++i)    {        if (used[i] != true)        {            used[i] = true;            ans[key] = i + 1;            dfs(key+1);            used[i] = false;        }    }}int main(){    scanf("%d",&n);    scanf("%d",&k);    dfs(0);    return 0;}

Java实现之方法一:

public class Solution {    List<List<Integer>> list = new ArrayList<List<Integer>>();    List<Integer> list1 = new ArrayList<Integer>();    int cnt;    //bigin is important here,n represent the avaiable integers for you to choose from    public void dfs(int level, int begin, int n){        if (level == cnt)        {            list.add(new ArrayList<Integer>(list1));            return;        }        for (int i = begin; i <= n; ++i){            list1.add(i);            dfs(level+1,i+1,n);            list1.remove(list1.size()-1);        }    }    public List<List<Integer>> combine(int n, int k) {        // write your code here        //set the global variable cnt to mark the amont        //or we can call like this:        //dfs(0,1,n,k)        cnt = k;        //when we have k integers to combine,in general        //conditions we let the parameter begin from 0        //when the depth(var:level) reach to k return        //in the below dfs() function,1 means the begin        dfs(0,1,n);        return list;    }}

Java实现之方法二

public class Solution {    /*Using array to implement it*/    public int key;    public static final int N = 1000000;    List<List<Integer>> list = new ArrayList<List<Integer>>();    int[] ans =new int[N];    //This method maybe a little bit difficult than the above    //For every element,we can choose use it or not    public void dfs(int level, int l,int r, int k)    {        //This place is a key point to finish the recursion         if (r - l + 1 < k)            return;        //when we've already chosen k integers        if (k == 0)        {     /*This point is also a key,we need to reallocate the memory space*/            List<Integer> list1 = new ArrayList<Integer>();            for (int i = 0; i < key; ++i)                list1.add(ans[i]);            list.add(list1);            return;        }        ans[level] = l;        //use the current element        dfs(level+1,l+1,r,k-1);        ans[level] = 0;        //not using        dfs(level,l+1,r,k);    }    public List<List<Integer>> combine(int n, int k) {        // write your code here        key = k;        dfs(0,1,n,k);        return list;    }}
1 0
原创粉丝点击