lintcode16- Permutations II- medium

Given a list of numbers with duplicate number in it. Find all unique permutations.

Example

For numbers [1,2,2] the unique permutations are:

[
  [1,2,2],
  [2,1,2],
  [2,2,1]
]

递归。和前面的差不多,但维护一个boolean[] isVisited数组以保证同一数字前后顺序唯一。当前面的2还没有使用的时候,就不应该让后面的2使用。

切记同元素问题处理要先sort!因为如果判断前后相同那要先把它们聚拢在一起。

public class Solution {
    /*
     * @param nums:  A list of integers
     * @return: A list of unique permutations
     */
    public List<List<Integer>> permuteUnique(int[] nums) {
        // write your code here
        List<List<Integer>> result = new ArrayList<List<Integer>>();

        if (nums == null){
            return null;
        }

        if (nums.length == 0){
            result.add(new ArrayList<Integer>());
            return result;
        }

        boolean[] isVisited = new boolean[nums.length];
        //切记要先sort!!!不然下面判断相邻的相同就不对了
        Arrays.sort(nums);
        helper(new ArrayList<Integer>(), isVisited, nums, result);
        return result;

    }

    private void helper(List<Integer> current, boolean[] isVisited, int[] nums, List<List<Integer>> result){
        if(current.size() == nums.length){
            result.add(new ArrayList<Integer>(current));
        }

        for(int i = 0; i < nums.length; ++i){
            if(isVisited[i] || (i > 0 && nums[i] == nums[i - 1] && !isVisited [i - 1])){
                continue;
            }
            isVisited[i] = true;
            current.add(nums[i]);
            helper(current, isVisited, nums, result);
            current.remove(current.size() - 1);
            isVisited[i] = false;
        }
    }
}
时间: 2024-10-27 00:16:04

lintcode16- Permutations II- medium的相关文章

Lintcode16 Permutations II solution 题解

[题目描述] Given a list of numbers with duplicate number in it. Find all unique permutations. 给出一个具有重复数字的列表,找出列表所有不同的排列. [题目链接] http://www.lintcode.com/en/problem/permutations-ii/ [题目解析] 跟 Permutations的解法一样,就是要考虑"去重".先对数组进行排序,这样在DFS的时候,可以先判断前面的一个数是否

LeetCode46,47 Permutations, Permutations II

题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) For example,[1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] LeetCode47 II Given a collection of numb

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

LeetCode 047 Permutations II

题目要求: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]. 代码如下: class Solution { public: vector

Permutations II leetcode java

题目: 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]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

LeetCode: Permutations II [046]

[题目] 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]. [题意] 给定一个候选数集合,候选集中可能存在重复数,返回所有的排列 [思路] 思路和Permutat

【LeetCode】Permutations II

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]. 首先分析一下与Permutations有何差异. 记当前位置为start,当前排列数

leetCode 47.Permutations II (排列组合II) 解题思路和方法

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]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

[LeetCode][JavaScript]Permutations II

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]. https://leetcode.com/problems/permutations

[leetcode] 040. Combination Sum II (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 040. Combination Sum II (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum-ii/ 代码(github):https://github.com/illuz/leetcode 题意: 跟 039 一样(给出一些正整数集合,