当前位置:网站首页>LeetCode_ 77_ combination
LeetCode_ 77_ combination
2022-07-19 11:50:00 【Fitz1318】
Topic link
Title Description
Given two integers n and k, Return range [1, n] All the possibilities in k Combination of numbers .
You can press In any order Return to the answer .
Example 1:
Input :n = 4, k = 2
Output :
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
Example 2:
Input :n = 1, k = 1
Output :[[1]]
Tips :
1 <= n <= 201 <= k <= n
Their thinking
backtracking
The combination problem is abstracted into the following tree structure , Each time you select an element from the collection , The selectable range shrinks with the selection , Adjust the selectable range . It can be found in the figure n It's the width of a tree ,k It's the depth of a tree . Every time a leaf node is searched in the figure , We found a result 
Retrospective trilogy
- Parameters and return values of recursive functions
- n,k,startIndex, Used to record recursion in this layer , Where the collection starts traversing
path: Used to store qualified resultsans: Set used to store qualified results
- Termination condition of backtracking function
- To the leaf node . That is to say
pathThe size of this array isk, It indicates that a subset size ofkA combination of , In the picturepathWhat you save is the path from the root node to the leaf node . - Use at this time
ansArray saves the results , And terminate the recursion of this layer
- To the leaf node . That is to say
- Single layer logic
- use
forLoop traversal , Specifically, every time fromstartIndexTo traverse the , And then usepathSave the retrieved nodei
- use
Pruning optimization

As shown in the figure , Where you can prune is at every level of recursion for Start position of cycle selection
- If for The number of elements after the starting position of the loop selection is not enough for us , Then there is no need to search
AC Code
class Solution {
public static List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> path = new ArrayList<>();
backTracing(n, k, 1, path, ans);
return ans;
}
private static void backTracing(int n, int k, int startIndex, List<Integer> path, List<List<Integer>> ans) {
// Termination conditions
if (path.size() == k) {
ans.add(new ArrayList<>(path));
return;
}
for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) {
path.add(i);
backTracing(n, k, i + 1, path, ans);
// to flash back
path.remove(path.size() - 1);
}
}
}
边栏推荐
- Unchangeable status quo
- 翻墙后看什么?最热门的国外网站——翻墙网址导航
- Resources for Physics based simulation in Computer Graphics 图形学中物理模拟的资源整理
- Robot development -- robot data summary
- A curated list of awesome Qt and QML
- 03-2、
- 夢想CMS 前臺搜索SQL注入
- TCP congestion control details | 7 Surpass TCP
- The concept of binary tree and three traversal methods (C language)
- Kernel mode and user mode
猜你喜欢

Two misunderstandings of digital transformation

466-82(3、146、215)

Detailed explanation of MySQL show processlist

Transport layer -------- TCP (I)

热议:老公今年已经34周岁想读博,以后做科研,怎么办?

03-1、内联函数、auto关键字、typeid、nullptr

Send blocking, receive blocking
![[PostgreSQL] PostgreSQL 15 optimizes distinct](/img/7c/89d05171902dd88bd2b5c352c3614f.png)
[PostgreSQL] PostgreSQL 15 optimizes distinct

Learning outline of the column "MySQL DBA's magic road"

Leetcode 1304. 和为零的 N 个不同整数
随机推荐
TiFlash 性能调优
百度文档翻译api
Wi Fi sensing technology and practice based on channel state information
Leetcode 1304. 和为零的 N 个不同整数
Tidb memory control document
jconsole线程面板中的阻塞总数和等待总数(转)
[binomial tree] the power of the button cattle customers must brush questions
Transport layer -------- TCP (I)
A simple websocket example
TiKV 内存参数性能调优
Opencv draw a black rectangle and write the serial number
【机器学习】多标签分类的评价指标与代码实现
LeetCode_216_组合总和Ⅲ
Dream CMS foreground search SQL injection
Wechat applet cloud development 1 - Database
03-1、内联函数、auto关键字、typeid、nullptr
Tikv thread pool performance tuning
TiKV 线程池性能调优
Round table record: fireside dialogue -- how to realize innovation in Web3
SQL UNION操作符