[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函数
发现函数可以设置属性变量, 如下 newfunc.func , newfunc.args def partial(func, *args, **keywords): """ ...
- Nginx无法监听虚拟VIP的问题报:99: Cannot assign requested address
99: Cannot assign requested address #本地网卡上没有10.0.0.3这个IPNginx就会报错: [root@lb01 conf]# /application/ng ...
- android 根据距离区分 点击跟滑动事件
public void onClick(View v) { if (isclick) Log.i(TAG, "onclick"); } }); } float distance = ...
- js根据当前日期 求一个月前 半年前 一年前的日期
function p(s) { return s < 10 ? '0' + s: s;}getlastmonth() function getlastmonth() { va ...
- Vue专题-路由系统
一切分离都是为了更好的结合,本文详细介绍了前后端分离架构之后,前端如果实现路由控制,即Vue路由系统. Vue路由系统 VueRouter实现原理 VueRouter的实现原理是根据监控锚点值的改变, ...
- 利用Matlab神经网络计算包预测近四天除湖北外新增确诊人数:拐点已现
数据来源: 国家卫健委 已经7连降咯! 1.20-2.10图示(更新中): 神经网络训练并预测数据: clear %除湖北以外全国新增确诊病例数 2020.1.20-2.9 num=[5,44,62, ...
- 计算机网络(4): socket select使用:聊天室模版
知识点: 如上所示,用户首先将需要进行IO操作的socket添加到select中,然后阻塞等待select系统调用返回.当数据到达时,socket被激活,select函数返回.用户线程正式发起read ...
- pc页面在移动端浏览时部分字体放大,与pc字体大小不一致(Font Boosting)
最近做一个页面时,需要pc的页面在移动端浏览时保持pc的样式缩小.但是发现部分文字被放大了.看上去特别诡异.如下图 绿框是PC端查看时的正常样式,红框是移动端看的字体放大的诡异样式 是什么原因呢? 后 ...
- JavaScript数组打平(4种方法)
let arr = [1, 2, [3, 4, 5, [6, 7, 8], 9], 10, [11, 12]]; flatten1 = arr => arr.flat(Infinity) fla ...
- PHP实现快速排序算法相关案例
<?php /** * 快速排序 --主要运用递归, 先把一个数找准位置,然后再递归把左右两边的数都找准位置 */ function QSort($a= []){ $nCount = count ...