LeetCode 077 Combinations
来源:互联网 发布:知乎 日本留学 编辑:程序博客网 时间:2024/05/22 15:32
题目
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
代码
1 典型的DFS,直接上代码
public class Solution { public ArrayList<ArrayList<Integer>> combine(int n, int k) { ArrayList<Integer> temp = new ArrayList<Integer>(); ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>(); useme(ans,temp,1,n,k); return ans; } public void useme(ArrayList<ArrayList<Integer>> ans, ArrayList<Integer> temp, int start, int n,int k){ if(k==0){ ans.add(new ArrayList<Integer>(temp)); return; } for(int i = start;i<=n;i++){ temp.add(i); useme(ans,temp,i+1,n,k-1); temp.remove(temp.size()-1); } }}
0 0
- LeetCode: Combinations [077]
- LeetCode 077 Combinations
- LeetCode 077 Combinations
- LeetCode 077 Combinations
- leetcode 077 Combinations
- leetcode 077 —— Combinations
- LeetCode: Combinations
- LeetCode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [LeetCode] Combinations
- Leetcode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [Leetcode] Combinations
- [leetcode] Combinations
- [LeetCode]Combinations
- [leetcode]Combinations
- GDAL GDALOpen 打开中文路径的问题
- 高版本EXCEL打开低版本工作簿的日期格式问题
- hdu 1693 Eat the Trees 插头dp入门题
- hdu1722 gcd
- linux ubuntu 安装 matlab 2010 及破解 详细图解
- LeetCode 077 Combinations
- hdu 1300 Pearls (dp)
- 将字符串切割成数组 componentsSeparatedByString
- HDU 4932 Miaomiao's Geometry 暴力
- hdu 4932 /bestcoder B题 #4 /思维题
- 连续子数组的最大和
- hdu 2187 悼念512汶川大地震遇难同胞——老人是真饿了
- Android中BroadCastReceiver使用(整理)
- java excel导入导出