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

Example:

  1. Input: [1,2,3]
  2. Output:
  3. [
  4. [1,2,3],
  5. [1,3,2],
  6. [2,1,3],
  7. [2,3,1],
  8. [3,1,2],
  9. [3,2,1]
  10. ]
  11.  
  12. Time: O(N!)
    Space: O(N)
  1. class Solution:
  2. def permute(self, nums: List[int]) -> List[List[int]]:
  3. res = []
  4. if nums is None or len(nums) == 0:
  5. return res
  6. my_set = set()
  7. cur_list = []
  8. self.dfs(0, my_set, nums, cur_list, res)
  9. return res
  10.  
  11. def dfs(self, level, my_set, nums, cur_list, res):
  12. if level == len(nums):
  13. res.append(list(cur_list))
  14. return
  15.  
  16. for i in range(len(nums)):
  17. if nums[i] in my_set:
  18. continue
  19. my_set.add(nums[i])
  20. cur_list.append(nums[i])
  21. self.dfs(level + 1, my_set, nums, cur_list, res)
  22. my_set.remove(nums[i])
  23. cur_list.pop()
  1. class Solution {
  2. public List<List<Integer>> permute(int[] nums) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. if (nums == null || nums.length == 0) {
  5. return res;
  6. }
  7. helper(res, new ArrayList<>(), nums);
  8. return res;
  9. }
  10.  
  11. private void helper(List<List<Integer>> res, List<Integer> list, int[] nums) {
  12. if (list.size() == nums.length) {
  13. res.add(new ArrayList<>(list));
  14. return;
  15. }
  16. for (int num: nums) {
  17. if (list.contains(num)) {
  18. continue;
  19. }
  20. list.add(num);
  21. helper(res, list, nums);
  22. list.remove(list.size() - 1);
  23. }
  24. }
  25. }

Solution 2:

  1. class Solution(object):
  2. def permute(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: List[List[int]]
  6. """
  7. res = []
  8. if nums is None or len(nums) == 0:
  9. return res
  10. self.dfs(nums, 0, res)
  11. return res
  12.  
  13. def dfs(self, array, level, res):
  14. if level == len(array):
  15. res.append(list(array))
  16. return res
  17.  
  18. for i in range(level, len(array)):
  19. array[level], array[i] = array[i], array[level]
  20. self.dfs(array, level + 1, res)
  21. 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. maven中的groupId和artifactId 区分

    原文地址:https://blog.csdn.net/snowin1994/article/details/53024871/ maven中的groupId和artifactId 区分 groupid ...

  2. Mybatis核心类生命周期和管理

    Mybatis核心类生命周期和管理 原文链接:https://blog.csdn.net/qq1134550437/article/details/51960480 1.SqlSessionFacto ...

  3. POJ 1185 状态DP

    这个题目是个挺难表示的状态DP,因为不但要考虑上下还要考虑左右,在DP里面就没有什么下了咯,但也至少除了考虑左右还要考虑上 所以先枚举出在同一行满足条件的状态 即 某状态 若 s&(s< ...

  4. Linux高性能服务器编程:高性能服务器程序框架

    服务器有三个主要模块: (1)I/O处理单元 (2)逻辑单元 (3)存储单元 1.服务器模型 C/S模型 逻辑:服务器启动后,首先创建一个或多个监听socket,并调用bind函数将其绑定到服务器感兴 ...

  5. docker---Dockerfile编写

    前言:镜像的定制实际上就是定制每一层所添加的配置文件,如果我们可以把每一层的修改.安装.构建.操作的命令都写入一个脚本,然后用这个脚本来构建.定制镜像,那么镜像构建透明性的问题.体积的问题就会得到解决 ...

  6. retrofit 上传文件 跟参数

    @Multipart @POST("postFied") Call<Void> postFied(@PartMap Map<String,String> m ...

  7. node,npm,webpack,vue-cli模块化编程安装流程

    首先什么都不要管,先装环境. pip是万能的!!! 安装node: pip3 install node 安装npm:   pip3 install npm 安装webpack: npm install ...

  8. openlayers的loaders方式加载

    openlayers loaders方式加载 let layerVector = new ol.layer.Vector({ source : new ol.source.Vector({ loade ...

  9. jquery 第一节 什么是jQuery

    简单来说,jQuery就是javascript的一个框架,也可以说是javascript的一个库.

  10. git配置报错fatal: Authentication failed for ''问题解决

    如果在git配置中报错fatal: Authentication failed for '',其实就是凭证失败的意思 接着输入一下命令行没有出现要求输入用户名或密码,并报错 $ git config ...