[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 ...
随机推荐
- UVALive 4329 树状数组第二题
大白书上的题目,比较巧妙的是其分析,为了求某个i点做裁判的时候的情况数,只要知道左边有多少比它小的记为ansc,右边有多少比它小的记为ansd,则总种数,必定为 ansc*(右边总数-ansd)+an ...
- MJJ玩磁铁
题目: Problem D: MJJ玩磁铁 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 139 Solved: 9[Submit][Status][ ...
- 深入分析Java反射(八)-优化反射调用性能
Java反射的API在JavaSE1.7的时候已经基本完善,但是本文编写的时候使用的是Oracle JDK11,因为JDK11对于sun包下的源码也上传了,可以直接通过IDE查看对应的源码和进行Deb ...
- leetcode中的sql
1 组合两张表 组合两张表, 题目很简单, 主要考察JOIN语法的使用.唯一需要注意的一点, 是题目中的这句话, "无论 person 是否有地址信息".说明即使Person表, ...
- 吴裕雄--天生自然TensorFlow2教程:创建Tensor
import numpy as np import tensorflow as tf tf.convert_to_tensor(np.ones([2, 3])) tf.convert_to_tenso ...
- 吴裕雄--天生自然 PHP开发学习:连接 MySQL、创建表
<?php $servername = "localhost"; $username = "root"; $password = "admin& ...
- mybatis自动扫描的时候,接口跟xml文件的名字最好能够一一对应
事实证明这是十分有好处的,当然,即便你不这么做,它也不一定会报invalid bound statement (not found),因为你不知道从哪儿拷来的配置文件可能从其他的地方做了配置,但是这么 ...
- goweb-go语言基础
go语言基础 虽然这本书是讲goweb,但还是吧go语言基础过了一遍,由于我之前已经对go语言基础做了一遍系统的学习,这里就当简单回顾一下,不再写过多笔记了,之前的写的博客都有基础知识,O(∩_∩)O ...
- 把cifar数据转换为图片
参考 https://gist.github.com/fzliu/64821d31816bce595a4bbd98588b37f5 """ make_cifar10.py ...
- xib下如何修改frame
1.取消xib下Use Auto Layout 2.xcode->product->clean