Leetcode 77. Combinations 组合

标签 : Leetcode


题目地址: https://leetcode-cn.com/problems/combinations/

题目描述

给定两个整数 nk,返回 1 ... n 中所有可能的 k 个数的组合。

示例:

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

算法思想

这个主要利用回溯法进行处理,我们用current数组保存当前遍历到的,如果len(current) ==k,那就说明当前已经够了,否则,则把当前的节点加入到current里面,外面的for循环的起始是在原有基础上加一,说明了之前遍历过的不能遍历,并且保证没有重复比如1 33 1.

python代码

class Solution(object):def dfs(self,i,n,k,res,current):if len(current) == k:res.append(copy.copy((current)))else:for tmp in range(i,n+1):current.append(tmp)self.dfs(tmp+1,n,k,res,current)current.pop()def combine(self, n, k):""":type n: int:type k: int:rtype: List[List[int]]"""res = []self.dfs(1,n,k,res,[])return res

Leetcode 77. Combinations 组合相关推荐

  1. 【DFS】LeetCode 77. Combinations

    LeetCode 77. Combinations Solution1:我的答案 DFS,时间复杂度O(n!)O(n!)O(n!),空间复杂度O(n)O(n)O(n) class Solution { ...

  2. LeetCode | 0077. Combinations组合【Python】

    LeetCode 0077. Combinations组合[Medium][Python][回溯] Problem LeetCode Given two integers n and k, retur ...

  3. LeetCode 77. Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  4. 77. Combinations(组合)

    题目链接:https://leetcode.com/problems/combinations/ 思路: 回溯,直接用回溯递归,注意边界条件. AC 1ms 100% Java: class Solu ...

  5. [leetcode] 77. Combinations @ python

    原题 Given two integers n and k, return all possible combinations of k numbers out of 1 - n. Example: ...

  6. LeetCode | 77. Combinations

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Exampl ...

  7. Leetcode(77)——组合

    Leetcode(77)--组合 题目 给定两个整数 n 和 k,返回范围 [ 1 , n ] [1, n] [1,n] 中所有可能的 k 个数的组合. 你可以按 任何顺序 返回答案. 示例 1: 输 ...

  8. LeetCode.77. 组合

    LeetCode.77. 组合 难度:中等 这道题属于回溯的经典题目: 回溯模板: void backtracking(参数) {if (终止条件) {存放结果;return;}for (选择:本层集 ...

  9. [LeetCode Python3]77. Combinations回溯

    77. Combinations class Solution:def __init__(self):self.res = []def trackback(self, track, index, n, ...

最新文章

  1. 全自动驾驶“生死时速”,特斯拉收购计算机视觉创企DeepScale
  2. 重新挂载linux分区添加硬盘
  3. Form学习入门系列(一)
  4. java中的操作符(位操作符>>与>>>的区别)
  5. C++中各种智能指针的实现及弊端(五)
  6. Flowable 数据库表结构 ACT_HI_VARINST
  7. Judy alpha 第七天
  8. python 鸭子类型_Python中的鸭子输入是什么?
  9. Android 音频开发(四) 如何播放一帧音频数据下
  10. Alfred Remote初体验
  11. 在C++ Builder6上使用Boost正则表达式库
  12. Atiit 常见功能 常用功能与模块的最快速解决方案
  13. 周记20180413
  14. 大学计算机基础及应用课后题答案第二版,《大学计算机基础》课后题答案完整版...
  15. MoveIt与RobotStudio
  16. 提高RM-MEDA局部学习(IRM-MEDA)
  17. Java使用Scanner与random实现骰子游戏
  18. d610网络计算机,尼康D610评测:机身细节
  19. Maven 跳过单元测试
  20. Libra R-CNN理解

热门文章

  1. RD650 raid5 linux,联想RD650服务器Raid5配置图文教程.docx
  2. netty内存泄露检测
  3. ORC提取图片中文字
  4. 我的理想计算机软件设计师,我的理想设计师
  5. Java实践课程设计附源码和sql脚本(物流管理系统)
  6. Android项目打包生成apk文件
  7. 物联网数据的十大特征
  8. python关联规则apriori算法_Python --深入浅出Apriori关联分析算法(二) Apriori关联规则实战...
  9. 信号隔离器在PLC/DCS控制系统的应用
  10. 使用mint ui(一)