Permutations II
来源:互联网 发布:php音频上传代码 编辑:程序博客网 时间:2024/05/17 08:18
Given a list of numbers with duplicate number in it. Find all unique permutations.
class Solution { /** * @param nums: A list of integers. * @return: A list of unique permutations. */ public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> results = new ArrayList<>(); if (nums == null) { return results; } if (nums.length == 0) { results.add(new ArrayList<Integer>()); return results; } List<Integer> result = new ArrayList<>(); Set<List<Integer>> hash = new HashSet<>(); int[] visited = new int[nums.length]; dfsHelper(nums, visited, results, result, hash); return results; } private void dfsHelper(int[] nums, int[] visited, List<List<Integer>> results, List<Integer> result, Set<List<Integer>> hash) { if (result.size() == nums.length ) { List<Integer> temp = new ArrayList<Integer>(result); if (!hash.contains(temp)) { results.add(temp); hash.add(temp); } } for (int i = 0; i < nums.length; i++) { if (visited[i] == 1) { continue; } result.add(nums[i]); visited[i] = 1; dfsHelper(nums, visited, results, result, hash); result.remove(result.size() - 1); visited[i] = 0; } }}
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Permutations and Permutations II
- Permutations && Permutations ii
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- Permutations II
- [bzoj2534]Uva10829L-gap字符串
- 二叉树2(二叉查找树的插入、查找、删除、遍历)
- java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
- css代码语法
- 你想知道的 Watch App 开发
- Permutations II
- nohup
- 逻辑回归模型(Logistic Regression, LR)
- 【机器学习实战】k-近邻算法
- linux之nfs服务器下载及配置
- 和像素工厂技术支持人员交流后有感
- poj3293(几何扫描线+并查集)
- qt designer设计的.ui文件转.py文件
- C语言itoa()函数和atoi()函数详解---整数与字符互相转换C函数