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
- LeetCode Combinations Problem using backtracing and DFS
- Leetcode dfs Combinations
- leetcode---combinations---dfs
- [LeetCode]problem 77. Combinations
- LeetCode-77-Combinations dfs+剪枝
- 【LeetCode】77. Combinations,DFS的变形应用
- LeetCode之Backtracing题目汇总
- Backtracing
- 【DFS】Combinations
- DFS Combinations
- leetcode之回溯backtracing专题1
- leetcode之回溯backtracing专题2
- leetcode之回溯backtracing专题3
- leetcode之回溯backtracing专题4
- leetcode之回溯backtracing专题5
- Leetcode dfs Letter Combinations of a Phone Number
- Leetcode Letter Combinations of a Phone Number DFS
- LeetCode Letter Combinations of a Phone Number DFS
- guava cache 创建方式 --- 1
- kafka本地存储1-配置说明
- USACO3.1Agrinet最小生成树
- Xargs用法详解
- 二进制,八进制,十进制,十六进制之间的转化
- LeetCode Combinations Problem using backtracing and DFS
- 改善Java Script程序的建议(一)
- "互联网+"系列(12) 微信:创造移动移动互联网的新生态 下篇
- oracle order by 拼音排序
- BitmapUtil处理工具类(圆角,模糊,黑白,存储指定路径)
- httpclient4.5 SSLPeerUnverifiedException
- leetcode笔记:Length of Last Word
- JAVA位运算符
- Android ViewPager 删除动画