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]
] Time: O(N!)
Space: O(N)
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res = []
if nums is None or len(nums) == 0:
return res
my_set = set()
cur_list = []
self.dfs(0, my_set, nums, cur_list, res)
return res def dfs(self, level, my_set, nums, cur_list, res):
if level == len(nums):
res.append(list(cur_list))
return for i in range(len(nums)):
if nums[i] in my_set:
continue
my_set.add(nums[i])
cur_list.append(nums[i])
self.dfs(level + 1, my_set, nums, cur_list, res)
my_set.remove(nums[i])
cur_list.pop()
 class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
helper(res, new ArrayList<>(), nums);
return res;
} private void helper(List<List<Integer>> res, List<Integer> list, int[] nums) {
if (list.size() == nums.length) {
res.add(new ArrayList<>(list));
return;
}
for (int num: nums) {
if (list.contains(num)) {
continue;
}
list.add(num);
helper(res, list, nums);
list.remove(list.size() - 1);
}
}
}

Solution 2:

class Solution(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
if nums is None or len(nums) == 0:
return res
self.dfs(nums, 0, res)
return res def dfs(self, array, level, res):
if level == len(array):
res.append(list(array))
return res for i in range(level, len(array)):
array[level], array[i] = array[i], array[level]
self.dfs(array, level + 1, res)
array[i], array[level] = array[level], array[i]

[LC] 46. Permutations的更多相关文章

  1. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

  2. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  3. 46. Permutations 排列数

    46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...

  4. 刷题46. Permutations

    一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...

  5. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  6. 46. Permutations 回溯算法

    https://leetcode.com/problems/permutations/ 求数列的所有排列组合.思路很清晰,将后面每一个元素依次同第一个元素交换,然后递归求接下来的(n-1)个元素的全排 ...

  7. LeetCode 【46. Permutations】

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

  8. 46. Permutations——本质和树DFS遍历无异 fun: for i in nums fun(i)

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

  9. 46. Permutations

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

随机推荐

  1. spyder.app制作图标

    安装了 anaconda3, 自带spyder, 但是只能在terminal 中打开, 非常不友好. 模仿 anaconda3/目录下 Anaconda-Navigator.app, 制作了 spyd ...

  2. App的布局管理

    今天学习了布局管理器,格局自己的学习内容和课程,我主要学习了两种管理布局方式 一:LinearLayout线性布局 线性布局是指布局里面的内容成线性排列,有两种排列方式,横向排列和纵向排列,而排列方式 ...

  3. python 2.7编译安装

    一 官网下载python2.7源码: python安装pip python -m ensurepip --default-pip

  4. Python内置文件

    概述 为了提升效率,Python有些内置文件如 __pycache__.py 详解 1)__pycache__.py, python程序运行时不需要编译成二进制代码,而直接从源码运行程序 Python ...

  5. Git&GitHub 基本使用

    如果是Windows用户需要自行去Git的官网下载Git工具 相信以您的聪明才知不需要一会就可以安装好Git工具了 博主这里是MacOS系统,本省就自带Git工具就不演示安装了 在完成一个简单的Git ...

  6. chrome安装switchyomega

    由于在国外网站找不到下载链接,在国内招了个crx文件,以下为安装crx教程 首先修改后缀为zip,再解压, 得到以下文件 然后在chrome里找到扩展程序, 打开开发者模式,点击-加载已解压的扩展程序 ...

  7. MySQL学习笔记——〇三 MySQL习题

    在前面讲了MySQL的初步使用方法以后,在这里放出来一些案例来看看怎么做. 先看看database的结构,一共5个表 外键关系: class的cid是student的class_id的外键,teach ...

  8. mac 下webstorm调节字体大小

    ctr+shift+A功能可以搜索对应功能. 在弹出框中输入Zoom可以轻松设置. 而且有关zoom的功能全部列出. 真是方便. 搜索到change font size with ctrl + whe ...

  9. Python 进阶 - 面向对象

    Python 面向对象 面向过程 把完成某个需求的所有步骤,从头到尾逐步实现 根据开发需求,将某些功能独立的代码封装成一个又一个函数 最后完成的代码,就是顺序地调用不同的函数 面向过程特点: 注重步骤 ...

  10. Mybatis入门——基础方式的增删该查、mapper动态代理方式的CRUD、类型转换器

    一.基础方式的增删该查: 1.mybatis约定:输入参数parameterType和输出参数resulrType在形式上只能有一个. 2.如果输入/输出参数:是简单类型(8个基本类型加String) ...