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

Example:

Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
思路
  对于排列组合问题首先应该想到使用递归思想来解决。另外还有一种非递归的解决办法。 解决代码

  递归方式
   图示步骤
解决代码


 class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return []
res = [] # 设置最终结果集
self.permution(nums, res, [])
return res def permution(self,nums, res_list, path):
if not nums: # 如果nums为空,则说明元素已经组合完毕,并将结果保存在结果集中(递归结束条件)
res_list.append(path)
return
for i in range(len(nums)): # 递归循环,nums[:i]+nums[i+1:] 表示将第nums[i]个元素加入到path中,在对剩下的元素进行递归。
self.permution(nums[:i]+nums[i+1:], res_list, path+[nums[i]])
循环方式
思路:主要思路是从一个元素开始,然后对其进行排列,然后第二个元素到来之后就变成两个元素进制排列。依此类推,当循环完毕之后,所有的元素就组合完毕。
图示步骤:

解决代码
 class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = [[]] # 设置一个空列表
for n in nums: # 从第一个元素开始遍历
tem_list = [] # 辅助列表,存储当前元素n和res中的列表的组合结果
for i in res: # 遍历res列表中每一种组合
for j in range(len(i)+1): # 求出res中每一个列表的长度并加一(为了对当前n进行重组).
tem_list.append(i[:j]+[n]+i[j:]) # 将当前n和res中的每一个列表进行重组。
res = tem_list # 赋值
return res # 返回结果 

【LeetCode每天一题】Permutations(排列组合)的更多相关文章

  1. [LeetCode] 系统刷题2_排列组合

    要用到backtracking,是否要跟backtracking放到一起总结? 适用范围: 几乎所有搜索问题 什么时候输出 哪些情况需要跳过 相关题目: [LeetCode] 78. Subsets ...

  2. 【LeetCode每天一题】Combinations(组合)

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

  3. LeetCode OJ:Combinations (排列组合)

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

  4. LeetCode 第17题--电话号码的组合(DFS)

    1. 题目 2.题目分析与思路 3.代码 1. 题目 输入:"23" 输出:["ad", "ae", "af", &qu ...

  5. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  6. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  7. 【LeetCode每天一题】Permutation Sequence(排列序列)

    The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...

  8. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  9. 【JavaScript】Leetcode每日一题-组合总和4

    [JavaScript]Leetcode每日一题-组合总和4 [题目描述] 给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target .请你从 nums 中找出并返回总和为 targ ...

随机推荐

  1. 轮滑基础(一)(前摔,葫芦步,推步,A字转弯,弓步转弯)

    轮滑新手入门推荐? [柚子陪你学轮滑轮滑教学]第一集 轮滑安全 1,站: 站立:脚可以成v字,或者平行,手放膝盖或者前伸.平行站立 膝盖相距一拳头左右,两腿间距略小于肩宽.膝盖略弯,腰下压,抬头挺胸 ...

  2. day_6.8 py 网络编程

    2018-6-8 18:20:30 OSI模型:就是七层物理层 ICMP 我ping你的时候要用,不仅要知道ip地址和网卡号mac地址 ARP  在我和你通讯前不知道的mac地址需要广播一下,当我说的 ...

  3. IBatisNet不常用到的配置(Dao.config ConnectionTimeout),居然不起作用(前辈留给我们的坑)

    IBattis 默认超时时间好像是30s,可多于这个时间总就会报错:DaoProxy : unable to intercept method name 'ExcuteQuery', cause : ...

  4. PAT甲级1061 Dating

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805411985604608 题意: 给定四个字符串. 前两个字符串 ...

  5. 初探Spring Cloud Config

    Spring Cloud Config提供了分布式系统中配置功能的服务端与客户端支持.对于不同环境的应用程序它的服务端提供了一种中心化的配置管理方式.并且其不仅适用于Spring的应用程序,其它语言开 ...

  6. Python的符号、对齐和用0填充

    # 用0填充 print("用0填充:{0:010.2f}".format(math.pi)) # 用1填充(事实上,你无法实现“用1填充”,因为即使实现了,那也是另外一个数字) ...

  7. [No0000180]改善C#程序的建议8:避免锁定不恰当的同步对象

    在C#中让线程同步的另一种编码方式就是使用线程锁.所谓线程锁,就是锁住一个资源,使得应用程序只能在此刻有一个线程访问该资源.可以用下面这句不是那么贴切的话来理解线程锁的作用:锁,就是让多线程变成单线程 ...

  8. sklearn的K折交叉验证函数KFold使用

    K折交叉验证时使用: KFold(n_split, shuffle, random_state) 参数:n_split:要划分的折数 shuffle: 每次都进行shuffle,测试集中折数的总和就是 ...

  9. 深入 Vue 生命周期

    深入 Vue 生命周期 这篇博客将会从下面四个常见的应用诠释组件的生命周期,以及各个生命周期应该干什么事 1.单组件的生命周期 2.父子组件的生命周期 3.兄弟组件的生命周期 4.宏mixin的生命周 ...

  10. Oracle安装部署之Win7下oracle11g数据库的安装及配置

    1.下载安装包 到oracle官网downloads下下载(第一次下载需要注册账号). Win7 64位下载64位oracle11g安装包 Win7 32位下载32位oracle11g(Oracle_ ...