LeetCode: Combinations 解题报告
Combinations
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],
]
SLOUTION 1:
一段时间不写真心容易不记得。
这是一道典型的模板题。以下是模板写法。
1. 必须时刻注意,for循环里 是i+1不是Index+1,这个老是会忘记。我们当前取过后,应该是处理下一个位置。
2. start 应该是从1开始。这个是这题的特别情况,一般idnex是0开始。
3. combination的题目,注意因为没有顺序,所以为了避免重复的一些解,我们只考虑递增的解。下一个排列题就不一样了。
// 注意,因为求的是组合,所以我们要考虑一下顺序问题,只需要考虑升序。这样的话就不会有重复的
// 的组合。
public void dfs(int n, int k, List<Integer> path, List<List<Integer>> ret, int start) {
if (path.size() == k) {
ret.add(new ArrayList<Integer>(path));
return;
} // 注意这里的条件i <= n 取n也是合法的!
// Example:
for (int i = start; i <= n; i++) {
path.add(i); // 注意,最后一个参数是i + 1,不是start + 1!!
dfs(n, k, path, ret, i + 1);
path.remove(path.size() - 1);
}
}
SLOUTION 2:
为了优化,在for 中加了一个判断,及时终止。i <= (n - k + 1)
// 注意,因为求的是组合,所以我们要考虑一下顺序问题,只需要考虑升序。这样的话就不会有重复的
// 的组合。
public void dfs2(int n, int k, List<Integer> path, List<List<Integer>> ret, int start) {
if (0 == k) {
ret.add(new ArrayList<Integer>(path));
return;
} // 注意这里的条件i <= n 取n也是合法的!
// Example:
for (int i = start; i <= (n - k + 1); i++) {
path.add(i); // 注意,最后一个参数是i + 1,不是start + 1!!
dfs(n, k - 1, path, ret, i + 1);
path.remove(path.size() - 1);
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/combination/Combine_1203.java
LeetCode: Combinations 解题报告的更多相关文章
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【LeetCode】77. Combinations 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- leetcode—Palindrome 解题报告
1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...
- LeetCode C++ 解题报告
自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- LeetCode: Subsets 解题报告
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- LeetCode: isSameTree1 解题报告
isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary t ...
- LeetCode: solveSudoku 解题报告
Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are in ...
随机推荐
- 修复windows Management Instrumentation(WMI)
第一步.重建repository文件夹下所有文件,打开“开始”“运行”输入一下命令并回车:1.停止 WMI 服务, net stop winmgmt2.删除repository文件夹下所有文件, %w ...
- ubuntu 下安装配置open-iscsi并自动挂载
1.安装open-iscsi apt-get install open-iscsi 2.发现iscsi-target(ISCSI服务器IP:192.168.1.104) iscsiadm -m dis ...
- Openstack网络相关概念比较复杂,经常使人混淆,本文进行相关说明。
Openstack网络相关概念比较复杂,经常使人混淆,本文进行相关说明. 文中相关术语与缩写 英文 缩写 中文 Virtual Local Area Network VLAN 虚拟局域网 Virtua ...
- tomcat占用cpu过高解决办法
在工作中经常遇到tomcat占用cpu居高不下,针对这种情况有以下处理办法进行排查. jps --> 查看java的进程 top -Hp pid --> 根据jps得到的进程号(pid), ...
- 开源的数据可视化JavaScript图表库:ECharts
ECharts (Enterprise Charts 商业产品图表库)是基于HTML5 Canvas的一个纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表.创新的拖拽 ...
- Java虚拟机学习 - 内存调优 ( 9 )
JVM调优主要是针对内存管理方面的调优,包括控制各个代的大小,GC策略.由于GC开始垃圾回收时会挂起应用线程,严重影响了性能,调优的目是为了尽量降低GC所导致的应用线程暂停时间. 减少Full GC次 ...
- ZOJ 3203 Light Bulb (三分查找)
Light Bulb Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...
- mysql 必须掌握的工具pt-query-digest安装
mysql 必须掌握的工具pt-query-digest 古人云:工欲善其事,必先利其器.作为一名优秀的mysql dba也需要有掌握几个好用的mysql管理工具,所以我也一直在整理和查找一些能够便于 ...
- 【php】基础学习3
本节主要是php中函数的学习: <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta http-equiv=Con ...
- SYS_R12 MOAC多组织底层技术实现技术分析(Oracle VPD) (案例)
2014-05-30 Created By BaoXinjian