惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Latest news
Latest news
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
B
Blog
T
Threat Research - Cisco Blogs
罗磊的独立博客
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
P
Palo Alto Networks Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
T
Tor Project blog
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
Recorded Future
Recorded Future
D
DataBreaches.Net
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
B
Blog RSS Feed
Scott Helme
Scott Helme
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
A
Arctic Wolf
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
N
Netflix TechBlog - Medium
L
LangChain Blog
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
M
MIT News - Artificial intelligence
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
W
WeLiveSecurity

Lan小站-嗯,不错! - 算法刷题

17. 电话号码的字母组合 - Lan小站-嗯,不错! 283. 移动零 - Lan小站-嗯,不错! 189. 轮转数组 - Lan小站-嗯,不错! 13. 罗马数字转整数 - Lan小站-嗯,不错! 14. 最长公共前缀 - Lan小站-嗯,不错! 28. 找出字符串中第一个匹配项的下标 双指针 - Lan小站-嗯,不错! 【Hot100】【一般】3. 无重复字符的最长子串 - Lan小站-嗯,不错! 【周赛】【简单】6362. 合并两个二维数组 - 求和法 - Lan小站-嗯,不错! 【简单】144. 二叉树的前序遍历 - Lan小站-嗯,不错!
77. 组合 - Lan小站-嗯,不错!
Lan · 2023-10-12 · via Lan小站-嗯,不错! - 算法刷题

Lan

本文最后更新于2023年10月12日,已超过975天没有更新,若内容或图片失效,请留言反馈。

给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。

你可以按 任何顺序 返回答案。

示例 1:

输入:n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
示例 2:

输入:n = 1, k = 1
输出:[[1]]

提示:

1 <= n <= 20
1 <= k <= n

# @Time    : 2023/10/12 16:43
# @Author  : Lan
# @File    : 77.组合.py
# @Software: PyCharm
"""
给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。
你可以按 任何顺序 返回答案。
"""
from typing import List


class Solution:
    result = []
    temp = []

    def backtracking(self, n, k, start):
        if len(self.temp) == k:
            self.result.append(self.temp.copy())
        else:
            for i in range(start, n - (k - len(self.temp)) + 2):
                self.temp.append(i)
                self.backtracking(n, k, i + 1)
                self.temp.pop()

    def combine(self, n: int, k: int) -> List[List[int]]:
        self.result = []
        if n == k:
            return [[i for i in range(1, n + 1)]]
        self.backtracking(n, k, 1)
        return self.result


print(Solution().combine(4, 2))