[LC] 46. 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]
- ]
- 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的更多相关文章
- LeetCode - 46. Permutations
46. Permutations Problem's Link -------------------------------------------------------------------- ...
- [Leetcode][Python]46: Permutations
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...
- 46. Permutations 排列数
46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...
- 刷题46. Permutations
一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- 46. Permutations 回溯算法
https://leetcode.com/problems/permutations/ 求数列的所有排列组合.思路很清晰,将后面每一个元素依次同第一个元素交换,然后递归求接下来的(n-1)个元素的全排 ...
- LeetCode 【46. Permutations】
Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...
- 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 ...
- 46. Permutations
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
随机推荐
- maven中的groupId和artifactId 区分
原文地址:https://blog.csdn.net/snowin1994/article/details/53024871/ maven中的groupId和artifactId 区分 groupid ...
- Mybatis核心类生命周期和管理
Mybatis核心类生命周期和管理 原文链接:https://blog.csdn.net/qq1134550437/article/details/51960480 1.SqlSessionFacto ...
- POJ 1185 状态DP
这个题目是个挺难表示的状态DP,因为不但要考虑上下还要考虑左右,在DP里面就没有什么下了咯,但也至少除了考虑左右还要考虑上 所以先枚举出在同一行满足条件的状态 即 某状态 若 s&(s< ...
- Linux高性能服务器编程:高性能服务器程序框架
服务器有三个主要模块: (1)I/O处理单元 (2)逻辑单元 (3)存储单元 1.服务器模型 C/S模型 逻辑:服务器启动后,首先创建一个或多个监听socket,并调用bind函数将其绑定到服务器感兴 ...
- docker---Dockerfile编写
前言:镜像的定制实际上就是定制每一层所添加的配置文件,如果我们可以把每一层的修改.安装.构建.操作的命令都写入一个脚本,然后用这个脚本来构建.定制镜像,那么镜像构建透明性的问题.体积的问题就会得到解决 ...
- retrofit 上传文件 跟参数
@Multipart @POST("postFied") Call<Void> postFied(@PartMap Map<String,String> m ...
- node,npm,webpack,vue-cli模块化编程安装流程
首先什么都不要管,先装环境. pip是万能的!!! 安装node: pip3 install node 安装npm: pip3 install npm 安装webpack: npm install ...
- openlayers的loaders方式加载
openlayers loaders方式加载 let layerVector = new ol.layer.Vector({ source : new ol.source.Vector({ loade ...
- jquery 第一节 什么是jQuery
简单来说,jQuery就是javascript的一个框架,也可以说是javascript的一个库.
- git配置报错fatal: Authentication failed for ''问题解决
如果在git配置中报错fatal: Authentication failed for '',其实就是凭证失败的意思 接着输入一下命令行没有出现要求输入用户名或密码,并报错 $ git config ...