LintCode : Permutations II

import java.util.Collections;
class Solution {
    /**
     * @param nums: A list of integers.
     * @return: A list of unique permutations.
     */
    public ArrayList<ArrayList<Integer>> permuteUnique(ArrayList<Integer> nums){
        // write your code here
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if (nums == null || nums.size() == 0) {
            return result;
        }
        Collections.sort(nums);
        boolean[] visited = new boolean[nums.size()];
        ArrayList<Integer> list = new ArrayList<Integer>();
        permutationHelper(result, list, nums, visited);
        return result;
    }

    private void permutationHelper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, ArrayList<Integer> nums, boolean[] visited) {
        if (list.size() == nums.size()) {
            result.add(new ArrayList<Integer>(list));
            return;
        }

        for (int i = 0; i < nums.size(); i++) {
            if (visited[i] == true || (i != 0 && nums.get(i) == nums.get(i - 1) && visited[i - 1] == false)) {
                continue;
            }
            visited[i] = true;
            list.add(nums.get(i));
            permutationHelper(result, list, nums, visited);
            list.remove(list.size() - 1);
            visited[i] = false;
        }
    }
}

  用visited数组来记录哪些是访问过的,visited[i - 1] == false很tricky,可能存在1222......这种情况,用第二个2递归的时候,前面那个2应该是还没有访问的,所以这个时候visited[i - 1] == false。

时间: 2024-11-01 22:49:47

LintCode : Permutations II的相关文章

[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

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

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刷题笔记】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]. 题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列: 上