LeetCode——Permutations II
来源:互联网 发布:杭州美工设计培训班 编辑:程序博客网 时间:2024/05/16 11:50
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 List<List<Integer>> permuteUnique(int[] num) {if (num == null)return null;List<List<Integer>> result = new ArrayList<List<Integer>>();if (num.length == 0)return result;Arrays.sort(num);permute(num, new boolean[num.length], new ArrayList<Integer>(), result);return result;}public void permute(int[] num, boolean[] isused,ArrayList<Integer> current, List<List<Integer>> result) {if (current.size() == num.length) {result.add(new ArrayList<Integer>(current));return;}for (int i = 0; i < num.length; i++) {if (!isused[i]) {isused[i] = true;current.add(num[i]);permute(num, isused, current, result);isused[i] = false;current.remove(current.size() - 1);while (i + 1 < num.length && num[i + 1] == num[i])i++;}}}
0 0
- LeetCode——Permutations II
- leetcode 047 —— Permutations II
- 【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——47——Permutations II
- [LeetCode]—Permutations II 求全排列(有重复值)
- Leetcode Problem.47—Permutations II C++实现
- LeetCode: Permutations II
- LeetCode Permutations II
- LeetCode: Permutations II
- [Leetcode] Permutations II
- [LeetCode] Permutations II
- [Leetcode] Permutations II
- 条款34:区分接口继承和实现继承
- BZOJ 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
- Miller_Rabin大素数测试与Pollard_rho整数分解模版
- 学生信息管理小程序
- IOS中常用的四种数据持久化方法简介
- LeetCode——Permutations II
- Fragment详细解说一
- Codeforces 463E Caisa and Tree dfs+分解质因素
- iOS 碰撞检测以及事件响应
- poj 1094 Sorting It All Out
- BZOJ 1610: [Usaco2008 Feb]Line连线游戏
- iOS开源免费接口
- dsfsdf
- Fragment详细解说二(android项目剖解之ViewPager+Fragment 实现tabhost效果 )