Permutations I & II

I

Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

public class Solution {
    public ArrayList<ArrayList<Integer>> permute(int[] nums) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if(nums==null||nums.length==0) return res;
        ArrayList<Integer> t = new ArrayList<Integer>();
        t.add(nums[0]);
        res.add(t);
        for(int i=1;i<nums.length;i++){
            res = helper(res, nums[i]);
        }
        return res;
    }
    private ArrayList<ArrayList<Integer>>  helper(ArrayList<ArrayList<Integer>> res, int k){
        ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
        for(int i=0;i<res.size();i++){
            for(int j=0;j<=res.get(i).size();j++){
                ArrayList<Integer> t = new ArrayList<Integer>(res.get(i));
                t.add(j,k);
                list.add(t);
            }
        }
        return list;
    }
}

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].

题解:加个hashset 去掉duplicate即可

public class Solution {
    public ArrayList<ArrayList<Integer>> permuteUnique(int[] nums) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if(nums==null||nums.length==0) return res;
        HashSet<ArrayList<Integer>> hs = new HashSet<ArrayList<Integer>>();
        ArrayList<Integer> it = new ArrayList<Integer>();
        it.add(nums[0]);
        res.add(it);
        for(int i=1;i<nums.length;i++){
            res = helper(hs, res, nums[i]);
        }
        return res;
    }
    private ArrayList<ArrayList<Integer>> helper(HashSet<ArrayList<Integer>> hs,ArrayList<ArrayList<Integer>> res, int k){
        ArrayList<ArrayList<Integer>> r = new ArrayList<ArrayList<Integer>>();
        for(int i=0;i<res.size();i++){
            for(int j=0;j<=res.get(i).size();j++){
                ArrayList<Integer> t = new ArrayList<Integer>(res.get(i));
                t.add(j,k);
                if(hs.contains(t))
                    continue;
                hs.add(t);
                r.add(t);
            }
        }
        return r;
    }
}
时间: 2024-10-03 14:56:06

Permutations I & II的相关文章

[LeetCode 46 &amp; 47] Permutations I &amp; II

题目链接:permutations 相似题型: 1. [LeetCode 39&40] Combination Sum I & II 2. [LeetCode 78] Subsets 3. [LeetCode 90] Subsets II 4. [LeetCode 22] Generate Parentheses 5. [LeetCode 77] Combinations import java.util.ArrayList; import java.util.List; /** * Gi

LeetCode (18) Permutations I &amp; II (排列一、二)

不存在重复的情况:题目描述 Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 本题要求输入一数组,输出数组的所有排列方式.题目中给定的为int类型的数组,有些题目中给定的为字符类型的,两种思路是一

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: 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]. 难度:80+15(怎样跳过重复的case来节省时间).我一开始的想法很简单,就是在permutation这道题的

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 I 和 II, N-Queens I 和 II,数独问题

转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the foll

【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,当前排列数