首页 > 代码库 > [LeetCode][I]Permutations II
[LeetCode][I]Permutations II
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 { boolean isUsed[]; List<List<Integer>> result; List<Integer> list; public List<List<Integer>> permuteUnique(int[] num) { isUsed = new boolean[num.length]; result = new ArrayList<List<Integer>>(); list = new ArrayList<Integer>(); Arrays.sort(num); permute(num); return result; } private void permute(int[] num) { if (list.size() == num.length) { result.add(new ArrayList<Integer>(list)); return; } for (int i = 0; i < num.length; i++) { if (isUsed[i]) continue; if (i > 0 && num[i - 1] == num[i] && (!isUsed[i - 1])) continue; isUsed[i] = true; list.add(num[i]); permute(num); list.remove(list.size() - 1); isUsed[i] = false; } } }
[LeetCode][I]Permutations II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。