[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 ...
随机推荐
- Python的安装部署
Python的安装部署 参考:https://www.runoob.com/w3cnote/pycharm-windows-install.html 参考:https://jingyan.baidu. ...
- python-IDE使用
集成开发工具 集成开发环境(IDE,Integrated Development Environment ) VIM 经典的linux下的文本编辑器,只有小白和的牛会用这个 Emacs linux文本 ...
- 详细的git入门级别,从安装到实战
拥有自己码云开源网站,想要上传项目到码云怎么操作?公司新技术提升由Svn转为Git,慌不慌?想要从Github开源网站下载开源项目,难道还依赖直接下载项目然后解压导入项目工程?下面可以通过及其简易且好 ...
- Activity组件:(一)通过显式意图和隐式意图来实现Activity间的跳转
一.通过显式意图来实现Activity间的跳转 显式意图是指在创建Intent对象时就指定接受者组件 /** * 下面是通过显式意图进行跳转,即明确写出要跳转到SecondActivity.class ...
- jsp/servlet编码原理
转自:http://janwer.iteye.com/blog/150226 首先,说说 JSP/Servlet 中的几个编码的作用 在 JSP/Servlet 中主要有以下几种设置编码的方式: pa ...
- PAT Advanced 1070 Mooncake (25) [贪⼼算法]
题目 Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many typ ...
- Linux 下载安装
安装教程:https://www.runoob.com/linux/linux-install.html Linux图形界面与命令行界面切换https://blog.csdn.net/ab522628 ...
- UML-领域模型-定义
领域模型是OO分析中最重要和经典的模型(用例是重要的需求分析制品,但不是面向对象的).领域模型也是重点. 1.关系 2.例子 3.定义 领域模型没有定义方法的类图.只包括: 1).领域对象或概念类 2 ...
- spark shc hbase 超时问题 hbase.client.scanner.timeout.period 配置
异常信息 20/02/27 19:36:21 INFO TaskSetManager: Starting task 17.1 in stage 3.0 (TID 56, 725.slave.adh, ...
- Deep learning with Python
一.导论 1.1 人工智能.机器学习.深度学习 人工智能.机器学习 人工智能:1980年代达到高峰的是专家系统,符号AI是之前的,但不能解决模糊.复杂的问题. 机器学习是把数据.答案做输入,规则作输出 ...