77. 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],
]

Hide Tags

Backtracking

链接:  http://leetcode.com/problems/combinations/

5/29/2017

25ms, 75%

 1 public class Solution {
 2     public List<List<Integer>> combine(int n, int k) {
 3         List<List<Integer>> ret = new ArrayList<>();
 4         if (n < k) return ret;
 5
 6         ArrayList<Integer> list = new ArrayList<Integer>();
 7         combination(n, k, ret, list, 1);
 8         return ret;
 9     }
10
11     private void combination(int n, int k, List<List<Integer>> ret, ArrayList<Integer> list, int pos) {
12         if (list.size() == k) {
13             ret.add(new ArrayList<Integer>(list));
14             return;
15         }
16         for (int i = pos; i <= n; i++) {
17             list.add(i);
18             combination(n, k, ret, list, i + 1);
19             list.remove(list.size() - 1);
20         }
21     }
22 }

别人的答案

这个不理解

https://discuss.leetcode.com/topic/12537/a-short-recursive-java-solution-based-on-c-n-k-c-n-1-k-1-c-n-1-k

iterative方法,还没有理解

https://discuss.leetcode.com/topic/8543/iterative-java-solution

Python神人

https://discuss.leetcode.com/topic/14626/1-liner-3-liner-4-liner

更多讨论

https://discuss.leetcode.com/category/85/combinations

时间: 2024-10-26 18:40:14

77. Combinations的相关文章

&lt;LeetCode OJ&gt; 77. Combinations

Total Accepted: 69360 Total Submissions: 206274 Difficulty: Medium 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], ]

【一天一道LeetCode】#77. Combinations

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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]

[Lintcode]152. Combinations/[Leetcode]77. Combinations

152. Combinations/77. Combinations 本题难度: Medium Topic: Search & Recursion Description Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example Given n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2]

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

LeetCode OJ - Symmetric Tree &amp;&amp; Same Tree

这两道题,大同小异. 我都是用BFS,在遍历的过程,判断结构是否相同/对称,值是否相同. 下面是AC代码: 1 /** 2 * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 3 * @param root 4 * @return 5 */ 6 public boolean isSymmetricRecursively(TreeNode root){ 7

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po

LeetCode OJ - Word Ladder 2

我发现在leetcode上做题,当我出现TLE问题时,往往是代码有漏洞,有些条件没有考虑到,这道题又验证了我这一想法. 这道题是在上一道的基础上进一步把所有可能得转换序列给出. 同样的先是BFS,与此同时需要一个hashMap记录下每个节点,和他所有父节点的对应关系,然后通过DFS,回溯所有可能的路径. 下面是AC代码. 1 /** 2 * Given two words (start and end), and a dictionary, find all shortest transform

【LeetCode OJ】Word Ladder I

Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in this problem: Hash Table. One hash set is the words dictionary where we can check if a word is in the dictionary in O(1) time. The other hash set is us

LeetCode OJ - Binary Tree Maximum Path

这道题需要注意的地方有以下一些: 1. 求从子树中的某节点到当前节点的最大路径不能采用递归方法,因为这个部分会被反复的调用,如果用递归,会使得之前已经计算过的节点被重复计算,使得时间复杂度特别高: 2. 树中有节点的值是负数的. 下面是AC代码.(我发现AC并不代表代码真的完全正确!!) 1 /** 2 * Given a binary tree, find the maximum path sum. 3 * The path may start and end at any node in t