leetcode47. Permutations II
来源:互联网 发布:解密码软件 编辑:程序博客网 时间:2024/06/05 17:51
47. 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], [2,1,1]]
解法
回溯法。使用used数据标记每个元素是否被访问过。
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1]) continue;用于去除重复的情况,比如[1, 1, 2], [1, 1, 2]
public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> permutes = new ArrayList<List<Integer>>(); if (nums == null || nums.length == 0) { return permutes; } List<Integer> permute = new ArrayList<Integer>(); boolean[] used = new boolean[nums.length]; Arrays.sort(nums); helper(permutes, permute, nums, used); return permutes; } private static void helper(List<List<Integer>> permutes, List<Integer> permute, int[] nums, boolean[] used) { // 出口 if (permute.size() == nums.length) { permutes.add(new ArrayList<Integer>(permute)); return; } // 过程 for (int i = 0; i < nums.length; i++) { if (used[i]) continue; if (i > 0 && nums[i] == nums[i - 1] && used[i - 1]) continue; used[i] = true; permute.add(nums[i]); helper(permutes, permute, nums, used); used[i] = false; permute.remove(permute.size() - 1); } }}
阅读全文
1 0
- [LeetCode47]Permutations II
- LeetCode47:Permutations II
- Leetcode47 Permutations II
- LeetCode47. Permutations II
- leetcode47. Permutations II
- LeetCode47 Permutations II
- leetcode47. Permutations II
- LeetCode47——Permutations II
- 递归——LeetCode47. Permutation II
- 【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
- oracleSQL优化方案
- 省队集训Round2 DAY1
- Oracle 中 decode 函数用法
- C#概述
- ThreadLocal解决事务线程安全问题(c3p0数据库连接池工具类)
- leetcode47. Permutations II
- CSS层叠顺序与层叠上下文
- Spring事务学习1
- Web 项目更改项目名
- Opencv学习笔记--图像处理的基本操作
- Unity3d同步读取APK包中的AssetBundle测试
- 【BJOI 2014】大融合
- (17/06/25)STM32学习->GPIO输出控制LED
- MyBatis实例