Combinations 解答
Question
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],
]
Solution
Draw out solution tree. Given [4, 2]
[]
/ / \ \
1 2 3 4
/ | \ / \ |
2 3 4 3 4 4
According to this tree, we can write out solution.
public class Solution {
public List<List<Integer>> combine(int n, int k) {
if (k > n)
k = n;
List<List<Integer>> result = new ArrayList<List<Integer>>();
dfs(n, k, 1, new ArrayList<Integer>(), result);
return result;
}
private void dfs(int n, int k, int start, List<Integer> list, List<List<Integer>> result) {
if (list.size() == k) {
result.add(new ArrayList<Integer>(list));
return;
}
for (int i = start; i <= n; i++) {
list.add(i);
dfs(n, k, i + 1, list, result);
list.remove(list.size() - 1);
}
}
}
Conclusion -- start pointer or visited[]
总结一下Backtracking这一类常见问题。
Backtracking一般都是应对穷举来找出所有答案的问题。对于这类问题,我们常常可以画出解空间树。然后遍历解空间树就可以得到答案。
遍历可以用 BFS 也可以DFS。一般来说,用迭代实现的DFS,不用自己维护栈所以是比较方便的做法。如果用BFS,则需要纪录一系列状态值。
在迭代的时候,我们要注意迭代结束之后要回到未迭代时的状态。
1. 如果有list操作,要把list的最后一个element移出
2. 如果有visited[],重新设置该element的状态为未访问过。
对比这些题目 Permutation, PhoneNumber,我们发现有的时候用到了start指针来代替visited[]数组表明该结点都无访问过。
那什么时候用start指针,什么时候用visited[]呢?
Combinations 解答的更多相关文章
- Letter Combinations of a Phone Number 解答
Question Given a digit string, return all possible letter combinations that the number could represe ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode OJ 77. Combinations
题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
随机推荐
- struts——拦截器
什么是拦截器 拦截器(Interceptor)是Struts 2的一个强有力的工具,有许多功能都是构建于它之上,如国际化(前两篇博客介绍过).转换器,校验等. 拦截器是动态拦截Action调用的对象. ...
- 使用WebRTC实现电脑与手机通过浏览器进行视频通话
最近一直在研究WebRTC,做了一个小项目:www.meet58.com,这个项目利用WebRTC.WebSocket可以让各种设备只通过浏览器进行视频聊天,无论是电脑.手机或者是平板.下面就是手机和 ...
- POJ训练计划2777_Count Color(线段树/成段更新/区间染色)
解题报告 题意: 对线段染色.询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话訪问线段树,求出颜色种数.结果超时了,最坏的情况下,染色能够染到叶子节点. 换成存下区 ...
- java集合总结【转】
Map.Set.Iterator迭代详解 Map接口定义了四种类型的方法,每个Map都包含这些方法. equals(Object o)比较指定对象与此Map的等价性. hashCode()返回此Map ...
- .NET批量大数据插入性能分析及比较
数据插入使用了以下几种方式 1. 逐条数据插入2. 拼接sql语句批量插入3. 拼接sql语句并使用Transaction4. 拼接sql语句并使用SqlTransaction5. 使用DataAda ...
- CSS的优先级
样式的优先级: (内联样式表[嵌入式样式])>(内部样式表)>(外部样式表) 经过测试动手测试发现有个(唯一的)例外 情况:当引用外部样式在内部样式表(非嵌入式样式)的后面时,外部样式会覆 ...
- SQL Server 2008 geometry 数据类型
摘自SQL Server 2008帮助 平面空间数据类型 geometry 是作为 SQL Server 中的公共语言进行时 (CLR) 数据类型实现的.此类型表示欧几里得(平面)坐标系中的数据. 注 ...
- 上传图片预览,支持IE6
//说明:图片上传预览插件 //上传的时候可以生成固定宽高范围内的等比例缩放图 //参数设置: //width 存放图片固定大小容器的宽 //height 存放图片固定大小容器的高 //imgDiv ...
- struts2中的action访问web对象
Struts2的Action就是一个普通的POJO对象,它和Web对象request.response.session和application没有耦合在一起,这样便于单独测试Action,那么我们在A ...
- 关于char/varchar(n)中n的探究:字符数or字节数
[问题来源]将设计的数据库表展示的时候,yu哥问我,你的那个top_info字段定义的类型是varchar(100),为什么是100呢,这100的长度能存多少个中文? 当时的想法就是,这个100能存多 ...