mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-25 09:42:16 +08:00
Merge branch 'master' into 添加0070组合优化Go版本
This commit is contained in:
@ -1,10 +1,10 @@
|
||||
<p align="center">
|
||||
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
|
||||
<a href="https://mp.weixin.qq.com/s/RsdcQ9umo09R6cfnwXZlrQ"><img src="https://img.shields.io/badge/PDF下载-代码随想录-blueviolet" alt=""></a>
|
||||
<a href="https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw"><img src="https://img.shields.io/badge/刷题-微信群-green" alt=""></a>
|
||||
<a href="https://img-blog.csdnimg.cn/20201210231711160.png"><img src="https://img.shields.io/badge/公众号-代码随想录-brightgreen" alt=""></a>
|
||||
<a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a>
|
||||
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
|
||||
</p>
|
||||
<p align="center"><strong>欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
|
||||
|
||||
@ -370,8 +370,45 @@ class Solution {
|
||||
|
||||
|
||||
Python:
|
||||
```python
|
||||
class Solution:
|
||||
result: List[List[int]] = []
|
||||
path: List[int] = []
|
||||
def combine(self, n: int, k: int) -> List[List[int]]:
|
||||
self.result = []
|
||||
self.combineHelper(n, k, 1)
|
||||
return self.result
|
||||
|
||||
|
||||
def combineHelper(self, n: int, k: int, startIndex: int):
|
||||
if (l := len(self.path)) == k:
|
||||
self.result.append(self.path.copy())
|
||||
return
|
||||
for i in range(startIndex, n - (k - l) + 2):
|
||||
self.path.append(i)
|
||||
self.combineHelper(n, k, i + 1)
|
||||
self.path.pop()
|
||||
```
|
||||
javascript
|
||||
```javascript
|
||||
let result = []
|
||||
let path = []
|
||||
var combine = function(n, k) {
|
||||
result = []
|
||||
combineHelper(n, k, 1)
|
||||
return result
|
||||
};
|
||||
const combineHelper = (n, k, startIndex) => {
|
||||
if (path.length === k) {
|
||||
result.push([...path])
|
||||
return
|
||||
}
|
||||
for (let i = startIndex; i <= n - (k - path.length) + 1; ++i) {
|
||||
path.push(i)
|
||||
combineHelper(n, k, i + 1)
|
||||
path.pop()
|
||||
}
|
||||
}
|
||||
```
|
||||
Go:
|
||||
```Go
|
||||
var res [][]int
|
||||
@ -392,7 +429,7 @@ func backtrack(n,k,start int,track []int){
|
||||
if len(track)+n-start+1 < k {
|
||||
return
|
||||
}
|
||||
for i:=start;i<=n;i++{
|
||||
for i:=start;i<=n;i++{
|
||||
track=append(track,i)
|
||||
backtrack(n,k,i+1,track)
|
||||
track=track[:len(track)-1]
|
||||
@ -401,7 +438,6 @@ func backtrack(n,k,start int,track []int){
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
Reference in New Issue
Block a user