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. Python赋值与深浅拷贝

    赋值: >> a = [1, 2, 3] >>> b = a >>> a = [4, 5, 6] //赋新的值给 a >>> a [4 ...

  2. Javascript合并表格相同内容单元格示例

    效果图: HTML代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  3. Unix api

    ● 线程 进程的所有信息都被自己的线程共享,包括代码.全局内存.堆.栈.文件描述符. 线程拥有自己的信息,包括线程ID.一组寄存器值.栈.调度优先级和策略.信号屏蔽字.errno变量以及线程的私有数据 ...

  4. MPD软件工作坊北京站:技术创新与研发效率带来的前沿思考

    在新技术层出不穷.不断迭代的当下,多数企业都在面临技术能力提升,认知升级等问题.面对技术企业的研发环节,为什么你的效率总是提不上来?都在寻找创新的技术领域,为何别人总能抢占先机?提升自己的研发竞争力, ...

  5. [No0000159]C# 7 中的模范和实践

    关键点 遵循 .NET Framework 设计指南,时至今日,仍像十年前首次出版一样适用. API 设计至关重要,设计不当的API大大增加错误,同时降低可重用性. 始终保持"成功之道&qu ...

  6. shell之awk面试小案例

    [root@chavinking mnt]# cat textfile chavinking 1 2 3 4 5 6 nope 1 2 3 4 5 6 [root@chavinking mnt]# c ...

  7. winform excel导入--NPOI方式

    项目中要用到excel导入数据,用NPOI方式做了一个demo,记录如下: Form1代码: public Form1() { InitializeComponent(); } private voi ...

  8. [apr] Apache Portable Runtime

    接手新项目,前任大量的使用了APR,虽然不影响理解主逻辑,但是看见一行不知道干嘛的代码,总是特别炸眼. 况且,虽然我从来不跨平台.但是APR如此优秀,学习理解它都不是浪费时间. 可是让人崩溃的是,竟然 ...

  9. 在dbgrideh中允许选择多行,如何知道哪些行被选中

    是个BOOKMARK类型的属性. SelectedRows: TBookmarkList procedure TForm1.Button1Click(Sender: TObject); var i, ...

  10. webpack打包配置模板

    /** * Created by zzq on 2017/3/26. *///__dirname是node.js中的一个全局变量,它指向当前执行脚本所在的目录module.exports = {//注 ...