(Leetcode)46&47 Permutations--LinkedList and HashSet

来源:互联网 发布:软件系统需求分析报告 编辑:程序博客网 时间:2024/04/30 09:53

Problem

46.Permutations

Given a collection of distinct numbers, return all possible permutations.

47. Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

Analysis

都是求一个数组的全排列。
46 没有重复数字,47有重复数字。

Solution

先求2个数的全排列,把第三个数插入上面的排列,知道nums[]所有的数都取完。
使用了LinkedList
47中为了去除相同的排列,用Hashset来做了一个标记,保证每个排列不重复。

Code

46

package _046Permutations;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;public class Solution {    public List<List<Integer>> permute(int[] nums) {        LinkedList<List<Integer>> permutations = new LinkedList<List<Integer>>();        if(nums==null)             return null;        LinkedList<Integer> firstnum = new LinkedList<Integer>();        firstnum.add(nums[0]);        permutations.add(firstnum);        for(int i=1; i<nums.length; i++){            while(permutations.peek().size()==i){                List<Integer> subPermutations = permutations.remove();                for(int j=0; j<=subPermutations.size(); j++){                    List<Integer> new_subPermutations = new LinkedList<Integer>(subPermutations);                    new_subPermutations.add(j, nums[i]);                    permutations.add(new_subPermutations);                }            }        }        return permutations;    }    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        int nums[] = {1,2,3};        List<List<Integer>> testList = new Solution().permute(nums);        for (int i = 0; i < testList.size(); i++) {            for (int j = 0; j < testList.get(i).size(); j++) {                System.out.print(testList.get(i).get(j)+" ");            }            System.out.println();        }    }}

47

package _47PermutationsII;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Set;public class Solution {    public List<List<Integer>> permuteUnique(int[] nums) {        Arrays.sort(nums);        LinkedList<List<Integer>> permutations = new LinkedList<List<Integer>>();        if(nums==null)             return null;        Set<String> markBoard = new HashSet<String>();        LinkedList<Integer> firstnum = new LinkedList<Integer>();        firstnum.add(nums[0]);        permutations.add(firstnum);        for(int i=1; i<nums.length; i++){            while(permutations.peek().size()==i){                List<Integer> subPermutations = permutations.remove();                markBoard.remove(subPermutations.toString());                for(int j=0; j<=subPermutations.size(); j++){                    List<Integer> new_subPermutations = new LinkedList<Integer>(subPermutations);                                                               new_subPermutations.add(j, nums[i]);                    if(markBoard.add(new_subPermutations.toString()))                        permutations.add(new_subPermutations);                }            }        }        return permutations;    }    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        int nums[] = {1,2,2,1};        List<List<Integer>> testList = new Solution().permuteUnique(nums);        for (int i = 0; i < testList.size(); i++) {            for (int j = 0; j < testList.get(i).size(); j++) {                System.out.print(testList.get(i).get(j)+" ");            }            System.out.println();        }        System.out.println(testList.size());    }}
0 0
原创粉丝点击