8.5 Combinations
来源:互联网 发布:firefox安装java插件 编辑:程序博客网 时间:2024/06/06 18:42
Link: https://oj.leetcode.com/problems/combinations/
Approach I: Recursive, DFS
I thinks the code is very similar to that of Permutations. We also add "item" into result when the item.size() == k (or equal to num.length).
The 1st difference is that we do not need to create a boolean array to denote whether an element has been used.
Second, we use a "start" pointer here, and use helper(n, k, i+1, item, result), because we need to move forward one each time.
{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} , {3, 4}. There is no combination in reverse order such as {2, 1}.
public class Solution { public ArrayList<ArrayList<Integer>> combine(int n, int k){ ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(n < k) return result; ArrayList<Integer> item = new ArrayList<Integer>(); dfs(n, k, 1, item, result); return result; } public void dfs(int n, int k, int start, ArrayList<Integer> item, ArrayList<ArrayList<Integer>> result){ if(item.size() == k){ result.add(new ArrayList<Integer>(item)); return; } for(int i = start; i <=n; i++){ item.add(i); dfs(n, k, i+1, item, result); item.remove(item.size()-1); } }}
Note: http://blog.csdn.net/u010500263/article/details/18435495 has a very good explanation.
"Parameters we need for a recursive call are the current combination, the combination result set, the n, the k, and the start point of iteration (e.g. if we chose 1 for the first position, start point of 2nd position is 2; while if we chose 2 for the first position, start point of second position is 3). "
Approach II: DFS, easier to understand
Idea: First get combine (n-1, k-1), then randomly pick a number and add to it
public class Solution { public ArrayList<ArrayList<Integer>> combine(int n, int k) { //This is similar to CC150 (9.4): to return all subsets of a set, but I didn't remember how to do it. //The difference is that in this question, k = 2 //so instead of getting all subsets, we only get those with 2 elements. //http://discuss.leetcode.com/questions/252/combinations if(n < k) return null; ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> list; if(k == 1){ for(int i = 1; i <=n; i++){ list = new ArrayList<Integer>(); list.add(i); lists.add(list); } return lists; } //k>=2 for(int i = n; i >=k; i--){//i>=k, not i>=1 for(ArrayList<Integer> oneList: combine(i-1, k-1)){ oneList.add(i); lists.add(oneList); } } return lists; }}
相关题目:
Subsets
本题可以看成是Subsets的特殊情况。当且仅当item.size() == k时,把item加入结果。
result.add(new ArrayList<Integer>(item));
- 8.5 Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- combinations
- Combinations
- Combinations
- Combinations
- 回调函数
- RIP协议两个版本对不连续子网的支持情况实验
- 函数指针和指针函数
- Android学习之路——Android四大组件之activity(一)
- Python 提取Twitter转发推文的元素(比如用户名)
- 8.5 Combinations
- BaseScreen
- ComponentFactory
- OneLineJTextArea
- WordInputJTextPane
- BaseDao
- 打发士大夫辅导费是
- BeanUtil
- 费鬼地方个梵蒂冈梵蒂冈