[LeetCode] Permutations II
来源:互联网 发布:阿里云虚拟主机 mysql 编辑:程序博客网 时间:2024/04/29 20:53
Total Accepted: 8649 Total Submissions: 35072
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,[1,1,2]
have the following unique permutations:[1,1,2]
, [1,2,1]
, and [2,1,1]
.
public class Solution { public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) { ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); dfs(num, list, 0); return list; } // swap each element into num[index] public void dfs(int[] num, ArrayList<ArrayList<Integer>> list, int index) { if(index == num.length) { ArrayList<Integer> path = new ArrayList<Integer>(); for (int i = 0; i < num.length; i++) path.add(num[i]); list.add(path); return; } for (int i = index; i < num.length; i++) { boolean isDup = false; for (int k = index; k < i ; k++ ) { if (num[k] == num[i]) isDup = true; } if (!isDup){ swap(num, index, i); dfs(num, list, index + 1); swap(num, index, i); } } } public void swap(int[] num, int a, int b) { int tmp = num[a]; num[a] = num[b]; num[b] = tmp; }}
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- Leetcode: Permutations and Permutations II
- leetcode-permutations and permutations II
- Leetcode:Permutations与Permutations II
- LeetCode: Permutations II
- LeetCode Permutations II
- LeetCode: Permutations II
- [Leetcode] Permutations II
- [LeetCode] Permutations II
- [Leetcode] Permutations II
- leetcode Permutations II
- [LeetCode]Permutations II
- leetcode - Permutations II
- LeetCode-Permutations II
- [leetcode] Permutations II
- EJB到底是什么
- ACM 算法类学习记录
- Cocos2dx 3.0 过渡篇(五) 随机数的获取
- 利用内存chunk充当数据buffer的vector的实现,和STL vector 有接口操作性能比较
- Spring中Controller和RequestMapping的详解
- [LeetCode] Permutations II
- php5.4配置Zend Guard Loader v3.3
- JSF2.0实战 - 3、onPageLoad
- Linux安装Tomcat7
- 通过网络访问adb
- error: Error retrieving parent for item: No resource found that matches the given name 'Widget.AppCo
- Android 数据库升级中数据保持和导入已有数据库
- C# UDP编程
- cocos2d-x 3.0正式版及android环境搭建详细教程