当前位置:网站首页>Leetcode 77 combination -- backtracking method
Leetcode 77 combination -- backtracking method
2022-07-01 16:35:00 【Hello, I'm Boger】
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/combinations
The question :
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 <= 20
1 <= k <= n
Ideas :
This topic is the topic of backtracking algorithm , About backtracking algorithms , You can read this article of code Capriccio 《 Theoretical basis of backtracking algorithm !》 Make a preliminary understanding .
Backtracking algorithm template framework :
void backtracking( Parameters ) {
if ( Termination conditions ) {
Store results ;
return;
}
for ( choice : The elements in this layer set ( The number of children in a tree is the size of the set )) {
Processing nodes ;
backtracking( route , Selection list ); // recursive
to flash back , Cancel processing result
}
}
This problem can be directly applied to the template of backtracking algorithm ( Picture transferred from code Capriccio )
As for the for In the loop i <= n - (k - path.size()) + 1
Why do you write like this , You can see Reference article Explanation of pruning optimization department in .
This topic Java Code :
class Solution {
List<List<Integer>> ans = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
private void backTracking(int n, int k, int start) {
if (path.size() == k) {
ans.add(new ArrayList<>(path));
return;
}
for (int i = start; i <= n - (k - path.size()) + 1; i++) {
path.add(i);
backTracking(n, k, i + 1);
path.removeLast();
}
}
public List<List<Integer>> combine(int n, int k) {
backTracking(n, k, 1);
return ans;
}
}
边栏推荐
- Analysis of PostgreSQL storage structure
- Five years after graduation, I became a test development engineer with an annual salary of 30w+
- [daily news]what happened to the corresponding author of latex
- There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode
- Go language source level debugger delve
- Principle of motion capture system
- Rhcsa Road
- 数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
- Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
- [JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境
猜你喜欢
运动捕捉系统原理
怎麼用MySQL語言進行行列裝置?
VMware 虛擬機啟動時出現故障:VMware Workstation 與 Hyper-v 不兼容...
PostgreSQL 存储结构浅析
Five years after graduation, I became a test development engineer with an annual salary of 30w+
IM即时通讯开发实现心跳保活遇到的问题
【直播预约】数据库OBCP认证全面升级公开课
Basic use of MySQL
Im instant messaging develops a message delivery scheme for 10000 people
How long will it take to achieve digital immortality? Metacosmic holographic human avatar 8i
随机推荐
你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
idea启动Command line is too long问题处理
Red team Chapter 10: ColdFusion the difficult process of deserializing WAF to exp to get the target
苹果自研基带芯片再次失败,说明了华为海思的技术领先性
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
Malaysia's Star: Sun Yuchen is still adhering to the dream of digital economy in WTO MC12
Which MySQL functions are currently supported by tablestore in table storage?
数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
广东用电量大跌,说明高新技术产业替代高能耗产业已取得初步成果
[nodemon] app crashed - waiting for file changes before starting... resolvent
[observation] where is the consulting going in the digital age? Thoughts and actions of softcom consulting
Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed
圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!
P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
Is it reliable to open an account on flush with mobile phones? Is there any potential safety hazard
Endeavouros mobile hard disk installation
部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...