zoukankan      html  css  js  c++  java
  • [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].

    https://oj.leetcode.com/problems/permutations-ii/

    思 路:有重复元素的数组生成permutation,要去除掉重复的情况:先排序,然后还是permutation的思路,依次往cur位置填元素,因为有 些元素有多次,所以需要用c1,c2来统计已经填好的数量和总共需要填的数量,并且因为排序了,相同元素的去重只需要跟前一个元素比较一下是否相等 (if (i == 0 || num[i] != num[i - 1]))。

    import java.util.ArrayList;
    import java.util.Arrays;
    
    public class Solution {
    	public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
    		if (num == null)
    			return null;
    		ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
    		if (num.length == 0)
    			return res;
    		Arrays.sort(num);
    		int[] permSeq = new int[num.length];
    		perm(num.length, 0, num, permSeq, res);
    		return res;
    	}
    
    	private void perm(int n, int cur, int[] num, int[] perm,
    			ArrayList<ArrayList<Integer>> res) {
    		if (cur == n) {
    			ArrayList<Integer> tmp = new ArrayList<Integer>();
    			for (int i = 0; i < perm.length; i++) {
    				tmp.add(perm[i]);
    			}
    			res.add(tmp);
    		} else {
    			int i;
    			for (i = 0; i < num.length; i++)
    				// this "if" is the key part
    				if (i == 0 || num[i] != num[i - 1]) {
    					int j;
    					int c1 = 0, c2 = 0;
    					for (j = 0; j < num.length; j++) {
    						if (num[i] == num[j])
    							c1++;
    					}
    
    					for (j = cur - 1; j >= 0; j--) {
    						if (perm[j] == num[i])
    							c2++;
    					}
    					if (c2 < c1) {
    						perm[cur] = num[i];
    						perm(n, cur + 1, num, perm, res);
    					}
    				}
    		}
    
    	}
    
    	public static void main(String[] args) {
    		System.out.println(new Solution().permuteUnique(new int[] { -1, 2, -1,
    				2, 1, -1, 2, 1 }));
    	}
    }

    第二遍记录:

     思路不变,改成java的语法来写,不用cur变量,通过tmp.size()判断。

    java

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Solution {
        public List<List<Integer>> permuteUnique(int[] num) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            if (num == null || num.length == 0)
                return res;
            List<Integer> tmp = new ArrayList<Integer>();
            Arrays.sort(num);
            permuteHelper(res, tmp, num);
            return res;
        }
    
        private void permuteHelper(List<List<Integer>> res, List<Integer> tmp, int[] num) {
            if (tmp.size() == num.length) {
                res.add(new ArrayList<Integer>(tmp));
                return;
            } else {
                for (int i = 0; i < num.length; i++) {
                    if (i == 0 || num[i] != num[i - 1]) {
                        int all = 0;
                        int cur = 0;
                        for (int j = 0; j < tmp.size(); j++) {
                            if (tmp.get(j) == num[i])
                                cur++;
                        }
                        for (int j = 0; j < num.length; j++) {
                            if (num[j] == num[i])
                                all++;
                        }
                        if (cur < all) {
                            tmp.add(num[i]);
                            permuteHelper(res, tmp, num);
                            tmp.remove(tmp.size() - 1);
                        }
    
                    }
                }
            }
    
        }
    
        public static void main(String[] args) {
            System.out.println(new Solution().permuteUnique(new int[] { 1, 1, 1, 2 }));
        }
    
    }

    C版本参考前一题 permutation。

    第三遍记录:

      注意all 和 cur的作用

      注意去重

  • 相关阅读:
    Windows常用命令
    Linux常用命令
    SpringBoot实现动态数据源切换及单库事务控制
    ArrayList与LinkedList
    java学习笔记之集合—ArrayList源码解析
    java学习笔记之反射—反射和工厂模式
    java学习笔记之反射—Class类实例化和对象的反射实例化
    java学习笔记之IO编程—对象序列化
    java学习笔记之IO编程—打印流和BufferedReader
    java学习笔记之IO编程—内存流、管道流、随机流
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3810756.html
Copyright © 2011-2022 走看看