Combinations leetcode java
题目:
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],
]
题解:
这道题就是用DFS(参考Work BreakII)的循环递归处理子问题的方法解决。n为循环的次数,k为每次尝试的不同数字。用到了回溯。
代码如下:
1 public ArrayList<ArrayList<Integer>> combine(int n, int k) {
2 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
3 if(n <= 0||n < k)
4 return res;
5 ArrayList<Integer> item = new ArrayList<Integer>();
6 dfs(n,k,1,item, res);//because it need to begin from 1
7 return res;
8 }
9 private void dfs(int n, int k, int start, ArrayList<Integer> item, ArrayList<ArrayList<Integer>> res){
if(item.size()==k){
res.add(new ArrayList<Integer>(item));//because item is ArrayList<T> so it will not disappear from stack to stack
return;
}
for(int i=start;i<=n;i++){
item.add(i);
dfs(n,k,i+1,item,res);
item.remove(item.size()-1);
}
}
Reference:http://blog.csdn.net/linhuanmars/article/details/21260217
Combinations leetcode java的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- [LeetCode][Java] Combinations
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- [LeetCode][Java] Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- C++雾中风景5:Explicit's better than implicit.聊聊Explicit.
关于Explicit还是Implicit一直是编程语言中能让程序员们干起架的争议.那些聪明的老鸟总是觉得Implicit的规则让他们能够一目十行,减少样板代码的羁绊.而很多时候,Implicit的很多 ...
- GPS数据包格式及数据包解析
GPS数据包解析 GPS数据包解析 目的 GPS数据类型及格式 数据格式 数据解释 解析代码 结构体定义 GPRMC解析函数 GPGGA解析函数 测试样例输出 gps数据包格式 gps数据解析 车联网 ...
- Java 中的国际化
国际化 ,英文叫 internationalization 单词太长 ,又被简称为 i18n(取头取尾中间有18个字母)不经大声呼喊 ,这都行 !接着看什么是国际化 , 国际化是指让产品或是程序在无需 ...
- [代码审计]XiaoCms(后台任意文件上传至getshell,任意目录删除,会话固定漏洞)
0x00 前言 这段时间就一直在搞代码审计了.针对自己的审计方法做一下总结,记录一下步骤. 审计没他,基础要牢,思路要清晰,姿势要多且正. 下面是自己审计的步骤,正在逐步调整,寻求效率最高. 0x01 ...
- [leetcode tree]95. Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx
从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...
- java知识回顾
一.构造方法能不能被继承 当然不能,1.构造方法是类的唯一入口 2.构造方法与类名相同 3.子类构造方法中隐式的调用了父类的构造方法 二.值传递和引用传递.不变类和可变类.直接赋值和浅拷贝和深拷贝 ...
- js ajax post 提交的时候后台接收不到参数,但是代码没有错,怎么回事
这个错误有两点,你自己写的php页面里面的参数接收出错了 还有就是你没有写一句重要的代码告诉浏览器 你使用post提交方式去提交 xhr.setRequestHeader("Content- ...
- 新型穿墙监控雷达Range-R:让你的隐私无所遁形(转)
还是隐私问题,原帖地址:http://www.freebuf.com/news/57446.html 在我们的认知中,政府对民众的监控已经成为一种常态.从电话.电子邮件到通信聊天.社交网络,一切细节都 ...
- 关于错误errno EFAULT:Bad address
UDP socket : read error Bad address 在写UDP server.在调用套接字读取的时候发生了这个错误. 通过看errno.h 能够看到相应的错误号 EFAULT: ...