[LC] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
] Time: O(N!)
Space: O(N)
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
res = []
if nums is None or len(nums) == 0:
return res
self.dfs(nums, 0, res)
return res def dfs(self, nums, level, res):
if level == len(nums):
res.append(list(nums))
return
my_set = set()
for i in range(level, len(nums)):
if nums[i] not in my_set:
my_set.add(nums[i])
nums[i], nums[level] = nums[level], nums[i]
self.dfs(nums, level + 1, res)
nums[i], nums[level] = nums[level], nums[i]
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> arrList = new ArrayList<>();
List<Integer> list = new ArrayList<>();
boolean[] visited = new boolean[nums.length];
Arrays.sort(nums);
helper(arrList, list, visited, nums);
return arrList;
} private void helper(List<List<Integer>> arrList, List<Integer> list, boolean[] visited, int[] nums) {
if (list.size() == nums.length) {
arrList.add(new ArrayList<>(list));
return;
}
for (int i = 0; i < nums.length; i++) {
if (visited[i] || (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1])) {
continue;
}
visited[i] = true;
list.add(nums[i]);
helper(arrList, list, visited, nums);
list.remove(list.size() - 1);
visited[i] = false;
}
}
}
[LC] 47. Permutations II的更多相关文章
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II (Back-Track, Sort)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II (JAVA)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode 【47. Permutations II】
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- Python的 5 种高级用法,效率提升没毛病!
任何编程语言的高级特征通常都是通过大量的使用经验才发现的.比如你在编写一个复杂的项目,并在 stackoverflow 上寻找某个问题的答案.然后你突然发现了一个非常优雅的解决方案,它使用了你从不知道 ...
- == 与 equals区别(HashCode方法)
1:==分析 1.2:基本类型比较 判断基本类型的数值是不是相等 1.3:对象类型比较 判断两个引用是不是指向同一个对象,即内存地址是不是相等. 2:equals分析 来判断对象内容是不是相等,一般有 ...
- [Java-基础] 注解
引言 在进行Spring Boot 入门的时候,主程序需要@SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用.这个的作用是什么?去掉的话会报错.如 ...
- Ubuntu的man中文包安装
apt-get install manpages-zh vi /etc/manpath.config :,$s#/usr/share/man#/usr/share/man/zh_CN#g 第一个命令: ...
- goweb-模板引擎
模板引擎 Go 为我们提供了 text/template 库和 html/template 库这两个模板引擎,模板引 擎通过将数据和模板组合在一起生成最终的 HTML,而处理器负责调用模板引擎并将引 ...
- javaweb学习——JDBC(五)
管理结果集 JDBC使用ResultSet来封装查询到的结果集,然后移动记录指针来取出结果集的内容,除此之外,JDBC还允许通过ResultSet来更新记录,并提供了ResultSetMetaData ...
- 60年前美国军方的这个编程原则,造就了多少伟大的框架--KISS原则
摘自:https://kb.cnblogs.com/page/654057/ 作者: 贺卓凡 来源: ImportSource 发布时间: 2020-01-23 19:52 阅读: 2324 次 ...
- Linux-编写简单守护进程
1.任何一个进程都可以将自己实现成一个守护进程 2.create_daemon函数要素 (1).子进程要等待父进程退出 (2).子进程使用setsid创建新的会话期,脱离控制台 (3).调用chdir ...
- c++语法(3)
子类覆盖父类的成员函数: #include "stdafx.h" #include "iostream" class CAnimal { protected: ...
- Django内置标签
在Django中也提供了大量Django自带的内置标签来供我们使用.标签的写法与过滤器的写法不同,标签是具有开始和结束的,例如:{% if %}为开始标签,{% endif %}为结束标签. 可以查 ...