【LeetCode每天一题】Permutations(排列组合)
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(排列组合)的更多相关文章
- [LeetCode] 系统刷题2_排列组合
要用到backtracking,是否要跟backtracking放到一起总结? 适用范围: 几乎所有搜索问题 什么时候输出 哪些情况需要跳过 相关题目: [LeetCode] 78. Subsets ...
- 【LeetCode每天一题】Combinations(组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
- LeetCode OJ:Combinations (排列组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- LeetCode 第17题--电话号码的组合(DFS)
1. 题目 2.题目分析与思路 3.代码 1. 题目 输入:"23" 输出:["ad", "ae", "af", &qu ...
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- 【JavaScript】Leetcode每日一题-组合总和4
[JavaScript]Leetcode每日一题-组合总和4 [题目描述] 给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target .请你从 nums 中找出并返回总和为 targ ...
随机推荐
- elasticsearch.yml配置文件
Elasticsearch的配置文件在config文件夹下,其中有elasticsearch.yml.logging.yml两个配置文件,其中elasticsearch.yml是用来配置Ela ...
- Python装饰器探险
关于python装饰器的理解和用法,推荐廖雪峰老师和这一篇博客以及知乎 以下代码均已手动敲过,看完本篇内容,包你装饰器小成! 装饰器实际上就是为了给某程序增添功能,但该程序已经上线或已经被使用,那么就 ...
- I - The 3n + 1 problem(2.4.2)
I - The 3n + 1 problem(2.4.2) Crawling in process... Crawling failed Time Limit:1000MS Memory Li ...
- Linux 下文件
Linux下文件的三个时间(Atime,Mtime,Ctime) [root@node ~]# stat install.log File: `install.log' Size: Blocks: ...
- centos 安装教程 服务器配置教程 服务器中安装python 服务器中安装Django 安装MySQL 配置MySQL
一 .解决python编译安装所需的软件依赖 yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel opens ...
- [No0000B8]WPF或Winform调用系统Console控制台显示信息
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...
- 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗? 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现! 是否有一个全局视角来查看系统的运行状况? 有什么办法可以监控到JVM的实时运行状态?
https://alibaba.github.io/arthas/ Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. 当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决 ...
- 1 byte 8 bit 1 sh 1 bit 2. 字符与编码在程序中的实现
https://en.wikipedia.org/wiki/Shannon_(unit) 1字节(英语:Byte)=8比特(英语:bit) The shannon (symbol Sh), also ...
- [skill] C与C++对于类型转换的验证
不多说了,代码说明一切. /home/tong/Src/copyleft/test [tong@T7] [:] > gcc .c /home/tong/Src/copyleft/test [to ...
- 完美解决failed to open stream: HTTP request failed!(file_get_contents引起的)
当使用php5自带的file_get_contents方法来获取远程文件的时候,有时候会出现file_get_contents(): failed to open stream: HTTP reque ...
解决代码