LeetCode No.77 Posted on 2021-04-07 | In OJ , LeetCode | | LeetCode第七十七题—组合 自己代码的开源仓库:click here 欢迎Star和Fork :) ¶题目描述 给定两个整数 n 和 k,返回 1 … n 中所有可能的 k 个数的组合。 123456789101112示例:输入: n = 4, k = 2输出:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],] ¶代码 1234567891011121314151617181920212223242526272829303132333435363738394041424344import copyclass Solution(object): def combine(self, n, k): """ :type n: int :type k: int :rtype: List[List[int]] 核心思想: 经典回溯法 def backtrack(path, selected): if 满足停止条件: res.append(path) for 选择 in 选择列表: 做出选择 递归执行backtrack 满足则return True 如果不满足要求就撤销选择 """ res = [] # 结果存储 numList = [i for i in range(1,n+1)] temp = [] def backtrack(temp,i,length): # 停止条件 if len(temp) == length: if temp not in numList: tt = copy.copy(temp) res.append(tt) return for j in range(i+1,n): # 做出选择 temp.append(numList[j]) # 递归执行 backtrack(temp,j,k) # 撤销选择 temp.pop() backtrack(temp,-1,k) return resif __name__ == '__main__': s = Solution() print(s.combine(4,2)) Hobby lead creation, technology change world. Post author: StriveZs Post link: www.strivezs.com/2021/04/07/LeetCode%E7%AC%AC%E4%B8%83%E5%8D%81%E4%B8%83%E9%A2%98/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.