Leetcode题库——46.全排列
@author: ZZQ
@software: PyCharm
@file: permute.py
@time: 2018/11/15 19:42
要求:给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
import copy
"""
思路一: DFS,去掉不满足条件的排列
"""
class Solution():
def __init__(self):
pass
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
ans = []
temp_ans = []
length = len(nums)
cur_length = 0
self.dfs(temp_ans, nums, cur_length, length, ans)
return ans
def dfs(self, temp_ans, nums, cur_length, length, ans):
if cur_length == length:
tt_ans = copy.deepcopy(temp_ans)
ans.append(tt_ans)
else:
for i in range(length):
if nums[i] not in temp_ans:
temp_ans.append(nums[i])
self.dfs(temp_ans, nums, cur_length+1, length, ans)
temp_ans.pop()
"""
思路二: 交换元素+DFS
"""
class Solution2():
def __init__(self):
pass
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums_len = len(nums)
ans = []
if nums_len == 0 or nums == []:
return []
self.exchange(nums, 0, nums_len, ans) # 采用前后元素交换的办法,dfs解题
return ans
def exchange(self, nums, i, len, ans):
if i == len-1: # 将当前数组加到结果集中
temp_list = []
for j in range(len):
temp_list.append(nums[j])
ans.append(temp_list)
return
# 将当前位置的数跟后面的数交换,并搜索解
for j in range(i, len):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
self.exchange(nums, i+1, len, ans)
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
Leetcode题库——46.全排列的更多相关文章
- Leetcode题库——47.全排列II
@author: ZZQ @software: PyCharm @file: permuteUnique.py @time: 2018/11/16 13:34 要求:给定一个可包含重复数字的序列,返回 ...
- leetcode题库
leetcode题库 #题名题解通过率难度出现频率 1 两数之和 46.5%简单2 两数相加 35.5%中等3 无重复字符的最长子串 31.1%中等4 寻找两个有序数组的中位 ...
- leetcode题库练习_数组中重复的数字
题目:数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次 ...
- leetcode题库解答源码(python3)
下面和大家分享本人在leetcode上已经ace的题目源码(python3): 本人会持续更新!- class Leetcode_Solution(object): def twoSum_1(self ...
- LeetCode题库整理(自学整理)
1. Two Sum 两数之和 来源:力扣(LeetCode) 题目:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利 ...
- LeetCode题库13. 罗马数字转整数(c++实现)
问题描述: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ...
- Leetcode题库——36.有效的数独
@author: ZZQ @software: PyCharm @file: leetcode36_isValidSudoku.py @time: 2018/11/19 19:27 要求:判断一个 9 ...
- Leetcode题库——49.字母异位词分组【##】
@author: ZZQ @software: PyCharm @file: leetcode49_groupAnagrams.py @time: 2018/11/19 13:18 要求:给定一个字符 ...
- Leetcode题库——48.旋转图像
@author: ZZQ @software: PyCharm @file: rotate.py @time: 2018/11/16 15:41 要求:给定一个 n × n 的二维矩阵表示一个图像.将 ...
随机推荐
- Vmware Vcenter6.5 全新安装及群集配置介绍
转 Vmware Vcenter6.5 全新安装及群集配置介绍 2016年12月31日 14:27:12 ccitzy01 阅读数:97772 标签: vmware [摘要] VMwarevCen ...
- C++虚函数再复习
- Chrome插件(扩展)开发全攻略
[干货]Chrome插件(扩展)开发全攻略:https://www.cnblogs.com/liuxianan/p/chrome-plugin-develop.html
- yarn的学习-1-包管理工具
https://yarn.bootcss.com 使用yarn的原因: 1.速度超快 Yarn 缓存了每个下载过的包,所以再次使用时无需重复下载. 同时利用并行下载以最大化资源利用率,因此安装速度更快 ...
- python面试题库——3数据库和缓存
第三部分 数据库和缓存(46题) 列举常见的关系型数据库和非关系型都有那些? 关系型数据库: Oracle.DB2.Microsoft SQL Server.Microsoft Access.MySQ ...
- OpenCV——创建Mat对象、格式化输出、常用数据结构和函数(point,vector、Scalar、Size、Rect、cvtColor)
创建Mat对象:
- PHP7.0新特性
http://blog.csdn.net/h330531987/article/details/74364681 反射 闭包 trait 还有数组
- CentOS中配置CDH版本的ZooKeeper
三台CentOS:Host0,Host1,Host2 在三台中分别安装zookeeper-server yum install zookeeper-server -y 修改zookeeper的配置文件 ...
- ASP.NET Core中,UseDeveloperExceptionPage扩展方法会吃掉异常
在ASP.NET Core中Startup类的Configure方法中,有一个扩展方法叫UseDeveloperExceptionPage,如下所示: // This method gets call ...
- 感言&3