`

Leetcode - Combination

 
阅读更多
[分析] 从 n 个数中取 k 个数,第一个数有 n 种取法……第 k 个数有 n - (k - 1)种取法,共需 k 层递归,第 i 层递归设置组合中第 i 个数的值。

public class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        recur(n, k, 1, new ArrayList<Integer>(), result);
        return result;
    }
    public void recur(int n, int k, int start, ArrayList<Integer> item, List<List<Integer>> result) {
        if (k == 0) {
            result.add((ArrayList<Integer>)item.clone());
            return;
        }
        for (int i = start; i + k - 1 <= n; i++) {
            item.add(i);
            recur(n, k - 1, i + 1, item, result);
            item.remove(item.size() - 1);
        }
    }
}
分享到:
评论

相关推荐

    數位之和leetcode-leetcode-cpp:我的LeetCodeC++答案

    combination sum: 39, 40, 216 - palindrome partitioning - regex - sudoku solver: 37 排序 - merge sort - quick sort - insertion sort - selection sort - counting sort 位操作 - find the only element which...

    leetcode怎么销号-LeetCode-Solutions:我自己的LeetCode解决方案

    leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag 0017 Letter Combinations of a Phone Number Medium 回溯、暴力 0034...

    leetcode分类-LeetCode-Java-Accepted:这是我的Java学习之旅

    leetcode 分类LeetCode-Java-接受 这是 Leetcode 问题的 Java 解决方案。 细节 标题和答案格式 /* * 17. Letter Combinations of a Phone Number * Target: Given a string containing digits from 2-9 inclusive, ...

    leetcode怎么计算空间复杂度是指-LeetCode-Solution:我的第一个LeetCode解决方案

    leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 already!) 我经常在递归的结束地方忘记return!!! 题型一:经典暴力递归:(里面涉及到重复不重复的...

    leetcode296-leetcode-in-py-and-go:Go中的Leetcode

    第 296 章PY 和 GO 中的 Leetcode 我在 Python Golang ...Leetcode ...40:combination-sum-ii:传递最后选择的索引 41:先缺失正,交换 42:只是提醒:块 - 垃圾箱 43:多字符串,i+j,i+j+1 44:通配符

    部分排序leetcode-Solved_coding_question_DSA:Solved_coding_question_DSA

    部分排序leetcode Solved_coding_question_DSA 在这里我们记录了所有最好的问题以供进一步参考,每个解决问题的详细信息如下 Count_number_of_ones.cpp:最长连续1的长度给定一个二进制字符串A,最多允许在0和1之间...

    LeetCode:用Java编写的LeetCode解决方案

    LeetCode 一、简介 Leetcode sol ution writtern in Java. ...combination -- leetcode 组合相关 ... etc 三、相关网页: 微博解题报告: 微博园解题报告: 算法辅导: Leetcode在线答案: 刷新题网站 :

    leetcode316-leetcode_problems:LeetCode刷题记录

    Combination Sum Description 给一组candidates (list of int)和target (int),求使用candidates内数字组合,让总和等于target的所有组合。 candidates内的数字皆不同 candidates内的数字可以重复使用无限次 ...

    leetcode打不开-leetcode:leetcode

    leetcode打不开Leetcode Note Tips Tip1: Two pointer for sorted array (#Array 1. Two Sum) Tip2: Sum[i:j] = Sum[0:j] - Sum[0:i] for continuous array (# Array 560. Subarray Sum Equals K) Tip3: Knapsack ...

    leetcode题库-little-algorithm:LeetCode题目参考代码与详细讲解,公众号《面向大象编程》文章整理

    leetcode题库 Little Algorithm 从 2020 年初开始,我在公众号《面向大象编程》上发表面试算法、LeetCode 题解相关文章,至今收获不少好评。此仓库是公众号内容的补充,包括公众号文章涉及到的题目的参考代码,以及 ...

    leetcode530-Leetcode:新的开始

    leetcode 530 力码 全部的: 易(173/237+x) 中(144/437+x) 硬(4/x) 问题 1.Two Sum(dict) 7.(跳过)(数学) 9.(跳过)(串串技巧) 11.盛水最多的容器 12.(跳过)(问题不好) 13.(跳过)(蛮力) 14.(跳过)...

    leetcode2-LeetcodeNotes:LeetCode解决方案和一切

    leetcode 2 Useful Links 我的题解 刷题打卡活动 算法基础课 算法基础课笔记 基础算法 : 快速排序,归并排序 : 整数二分,浮点数二分 数据结构 搜索与图论 : Dijkstra, Bellman-Ford, SPFA, Floyd : 染色法、匈牙利...

    leetcode2sumc-Leetcode_imp_C:Leetcode在C上的实现

    leetcode 2 和 c Leetcode_imp_C Leetcode 在 C 上的实现 大批: leetcode_0001_two_sum.c leetcode_0011_max_area.c leetcode_0015_three_sum.c ...leetcode_0039_combination_sum.c 40 leetcode_0041_first_miss

    gasstationleetcode-Leetcode:力码

    Leetcode\combination sum(39).swift Leetcode\count number of team(1395).swift Leetcode\counting bits(338).swift Leetcode \find 数组中的所有重复项(442).swift Leetcode\find peak element(162).swift ...

    leetcode双人赛-Leetcode:leetcode中问题的解答

    leetcode双人赛力码 你可以在leetcode中找到一些问题的答案,你可以在leetcode中搜索问题名称,然后就会找到解决方案的代码 leetcode 链接 如果你对我的 leetcode 个人资料感兴趣,你可以去 from math import log ...

    leetcode最大蓄水量-leetcode_note_book:leetcode题目分类及刷题总结

    CombinationSum 组合之和 完成 LinkedList 题目 说明 状态 AddTwoNumber 两数相加 完成 SwapPairs 两两交换链表中的节点 完成 String 题目 说明 状态 LongestSubstring 最长子串 完成 LongestPalindrome 最长回文...

    leetcode2sumc-CTCI:CTCI

    leetcode 2 和 c CTCI 回溯总结: 组合和、子集、排列、回文) 组合和 def combinationSum ( self , candidates , target ): def backtrack ( tmp , start , end , target ): if target == 0 : ans . append ( tmp ...

    javalruleetcode-LeetCode:力码

    java lru leetcode LeetCode Language ...Combination 组合和:15,16,18,39,40 Divde and Conquer 分治:4,23,33,34,81,108,109,167,278,307,324 Dyanmic 动态规划:11,42,44,62-64,72,87,91,93,97,115

    Coding Interview In Java

    leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder ...

    cpp-算法精粹

    AlgoHub囊括了 POJ, ZOJ, leetcode, HackerRank 等网站的经典题目(一些质量不高的题目则忽略),且 AlgoHub有非常简单的加题系统,用户不需要写一行代码即可自己添加题目,所以AlgoHub的题库还在飞速增长中。...

Global site tag (gtag.js) - Google Analytics