Permutations II
来源:互联网 发布:淘宝寄快递在哪里 编辑:程序博客网 时间:2024/06/05 09:08
题目:
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]
.
貌似做过,不造怎么又跳出来了,但是跳出来的时候有代码,然后我就修改了下,然后竟然成功了yeah!!
原代码:
public static List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> list = new ArrayList<List<Integer>>(); boolean[] visited = new boolean[nums.length]; List<Integer> inner = new ArrayList<Integer>(); permuteUnique(nums, 0, inner, visited, list); return list; } public static void permuteUnique(int[] nums, int index, List<Integer> inner, boolean[] visited, List<List<Integer>> list) { if(inner.size() == nums.length){ if(!list.contains(inner)){ list.add(new ArrayList<Integer>(inner)); } return; } for(int i = index; i < nums.length; i++){ if(i != index && find(nums, index, i, nums[i])) if(visited[i])continue; visited[i] = true; inner.add(nums[i]); permuteUnique(nums, index + 1, inner, visited, list); visited[i] = false; inner.remove(inner.size() - 1); } }private static boolean find(int[] nums, int s, int e, int target) {// TODO Auto-generated method stubfor (int i = s; i < e; i++) {if (nums[i] == target) {return true;}}return false;}
修改过后的代码:
public class Solution { public static List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> list = new ArrayList<List<Integer>>(); Arrays.sort(nums); boolean[] visited = new boolean[nums.length]; List<Integer> inner = new ArrayList<Integer>(); permuteUnique(nums, inner, visited, list); return list; } public static void permuteUnique(int[] nums, List<Integer> inner, boolean[] visited, List<List<Integer>> list) { if(inner.size() == nums.length){ if(!list.contains(inner)){ list.add(new ArrayList<Integer>(inner)); } return; } for(int i = 0; i < nums.length; i++){ if(i >= 1 && !visited[i - 1] && nums[i] == nums[i - 1]) continue; if (visited[i])continue; visited[i] = true; inner.add(nums[i]); permuteUnique(nums, inner, visited, list); visited[i] = false; inner.remove(inner.size() - 1); } }}
这里用 if(i >= 1 && !visited[i - 1] && nums[i] == nums[i - 1])代替if(i > index && nums[i] == nums[i - 1])
之前有每次i从index下标开始遍历,可以保证前面重复的元素已经用过一次了,而这里每次从下标0开始,如果不用!visited[i - 1],就会导致重复的元素第一次被调用的时候,就跳过去了,即如果是【1,1】的话,访问第2个1的时候,第1个1已经访问过了,visited[i - 1] = true,直接continue了,会返回空。
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
- java:水仙花数打印
- 类和对象浅谈(1)
- [PAT]1102. Invert a Binary Tree (25)
- Ajax获取XmlHttpRequest对象的方法,兼容IE、火狐。用来与服务器进行通信。
- Scala学习笔记02【数组、列表、元组、集合和映射】
- Permutations II
- 回答“为什么跳槽”
- C#编程思想及C#类型/运算符和强制类型转换
- Objective-C占位符(转载)
- 黑马程序员——Java语言:正则、反射
- JAVA中文字符编码问题详解
- 实习期间初次使用javascript调用webserver,将html页面提交
- springMVC学习指南<附录>tomcat的使用
- 1