广告
返回顶部
首页 > 资讯 > 后端开发 > Python >【leetcode】 46. Permu
  • 863
分享到

【leetcode】 46. Permu

leetcodePermu 2023-01-31 04:01:45 863人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

Permutations Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] hav

  1. Permutations

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

给一堆不同的数,然后让你写成这样上面的样子,就是排列组合。
举个简单例子【1,2,3】吧
刚拿到这3个数
假设开始我们什么都没有,从一个空[]开始,
那现在第一步,我们有三条路:[1],[2],[3]
然后 对[1]来说,有两条路[1,2] ||[1,3],然后这两条路分别只有一条路[1,2,3] ||[1,3,2]
以此类推

这里写图片描述

代码如下:用递归




class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        self.res = []
        sub = []
        self.dfs(nums,sub)
        return self.res

    def dfs(self, Nums, subList):
        if len(subList) == len(Nums):
            #print res,subList
            self.res.append(subList[:])
        for m in Nums:
            if m in subList:
                continue
            subList.append(m)
            self.dfs(Nums,subList)
            subList.remove(m)

--结束END--

本文标题: 【leetcode】 46. Permu

本文链接: https://www.lsjlt.com/news/187763.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 【leetcode】 46. Permu
    Permutations Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] hav...
    99+
    2023-01-31
    leetcode Permu
  • C++实现LeetCode(46.全排列)
    [LeetCode] 46. Permutations 全排列 Given a collection of distinct integers, return a...
    99+
    2022-11-12
  • 46云服务器
    66.66 GHz的处理器性能约为每秒 6.4 GB 的数据处理能力,相当于每秒 10 GB 以上的传输能力。在 66 66.66 GHz的处理器上运行的云计算平台通常需要使用高速网络连接、大型内存和大量的 CPU 资源来实现高效的应用程序...
    99+
    2023-10-26
    服务器
  • 46. Python Socket编程
    复习:消息队列为了防止消息丢失,或者是调用方,不需一直等待响应方的结果。# threadtest.pyimport codecs from queue import Queue from threading import Thread i...
    99+
    2023-01-31
    Python Socket
  • 46云服务器怎么样
    一、选择合适的云服务器 首先,我们需要了解云服务器的基本参数和特点。以下是云服务器的主要参数: CPU:处理器是云服务器的核心,它决定了服务器的性能和处理能力。Intel Core i5、i7和AMD Ryzen是常见的处理器品牌,不同...
    99+
    2023-10-28
    服务器
  • PostgreSQL DBA(46) - PG Operator classes and families
    先前章节已简单提到pg_am,以...
    99+
    2022-10-18
  • Python爬虫入门教程 46-100
    1. 手机收音机-爬前叨叨 今天选了一下,咱盘哪个APP呢,原计划是弄荔枝APP,结果发现竟然没有抓到数据,很遗憾,只能找个没那么圆润的了。搜了一下,找到一个手机收音机 下载量也是不错的。 2. 爬虫套路 爬虫基本套路 抓包获取链接 ...
    99+
    2023-01-30
    爬虫 入门教程 Python
  • ORA-00600: internal error code, arguments: [keltnfy-ldmInit], [46], [1], [], [], []
     今天建库的时候忽然报错ORA-00600: internal error code, arguments: [keltnfy-ldmInit],...
    99+
    2022-10-18
  • skipping archived logs of thread 1 from sequence 29 to 46; already backed up
    问题描述:删除归档的备份,在进行归档的重新备份,提示:skipping archived logs of thread 1 from sequence 29 to 46; already backed up,这个也不算报错,接着之前的ORA...
    99+
    2020-07-08
    skipping archived logs of thread 1 from sequence 29 to 46; already backed up
  • Java日常练习题,每天进步一点点(46)
    目录1、设Tree为已定义的类名,下列语句能正确创建 Tree 对象的是 。2、区分类中重载方法的依据是( )。3、以下代码执行后输出结果为( )4、现有一变量声明为 boolean...
    99+
    2022-11-12
  • PostgreSQL 源码解读(46)- 查询语句#31(query_planner函数#7)
    先前的章节已介绍了函数query_planner中子函数reconsider_outer_join_clauses和generate_base_implied_equalities...
    99+
    2022-10-18
  • PostgreSQL 源码解读(61)- 查询语句#46(make_one_rel函数#11-...
    本节继续介绍make_one_rel函数中的set_base_rel_pathlists->create_tidscan_paths函数,该函数创建相应的TID扫描路径。 一...
    99+
    2022-10-18
  • docker笔记46-调整客户端rbd块设备的大小
    调整客户端rbd块设备的大小    接上一小节,我们挂载了一个rbd块设备到/mnt下,其大小为1个G:[root@k8s-master1 ~]# df -h ...
    99+
    2023-06-04
  • leetCode
    two sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You m...
    99+
    2023-01-31
    leetCode
  • [leetcode]39. Combin
    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the...
    99+
    2023-01-31
    leetcode Combin
  • python(leetcode)-344
    编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 你可以假设数组中的所有字符都是 ASCII...
    99+
    2023-01-30
    python leetcode
  • leetcode 3. Longest
    题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest subst...
    99+
    2023-01-31
    leetcode Longest
  • leetcode 241. Differ
    题目:Given a string of numbers and operators, return all possible results from computing all the different possible ways t...
    99+
    2023-01-31
    leetcode Differ
  • [LeetCode Python3]5
    在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据。 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的行数和列数。 重构后的矩阵需要将原始矩...
    99+
    2023-01-31
    LeetCode
  • python(leetcode)-283
    给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少...
    99+
    2023-01-30
    python leetcode
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作