Permutations II - Leetcode
2015-02-19 03:34
344 查看
<pre name="code" class="java"><pre name="code" class="java">public class Solution { public List<List<Integer>> permuteUnique(int[] num) { List<List<Integer>> result = new ArrayList<>(); List<Integer> path = new ArrayList<>(); boolean[] visited = new boolean[num.length]; Arrays.sort(num); DFS(num, path, result, visited); return result; } private void DFS(int[] num, List<Integer> path, List<List<Integer>> result, boolean[] visited){ if(path.size() == num.length){ ArrayList<Integer> al = new ArrayList<>(path); result.add(al); return; } for(int i=0; i<num.length; i++) { if(i>0 && num[i]==num[i-1] && visited[i-1]==false) continue; if(!visited[i]){ visited[i]=true; path.add(num[i]); DFS(num, path, result, visited); path.remove(path.size()-1); visited[i]=false; } } } }
分析:这个思路是看的这里的:http://www.cnblogs.com/springfor/p/3898447.html
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].
相关文章推荐
- 【LeetCode-47】Permutations II
- leetcode47. Permutations II
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
- LeetCode 047 Permutations II
- LeetCode-- Permutations II
- LeetCode--47. Permutations II
- 【LeetCode】Permutations II
- LeetCode | Permutations II
- LeetCode Permutations II
- [LeetCode] Permutations II 解题报告
- [LeetCode] Permutations II 解题报告
- LeetCode – Refresh – Permutations II
- 【Leetcode】Permutations II
- leetcode. Permutations && Permutations II
- 【LeetCode】Permutations II
- [C++]LeetCode: 120 Permutations II
- leetcode: Permutations II
- leetcode 46-Permutations and 47-Permutations II
- leetcode-47. Permutations II(重复元素全排列)
- 119_leetcode_Permutations II