L1:06 - Permutations II

来源:互联网 发布:淘宝联盟高级账户佣金 编辑:程序博客网 时间:2024/06/13 21:08

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


/** * Copyright: NineChapter * - Algorithm Course, Mock Interview, Interview Questions * - More details on: http://www.ninechapter.com/ */public class Solution {    public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();        if(num == null || num.length == 0)            return result;        ArrayList<Integer> list = new ArrayList<Integer>();        int[] visited = new int[num.length];                Arrays.sort(num);        helper(result, list, visited, num);        return result;    }        public void helper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int[] visited, int[] num) {        if(list.size() == num.length) {            result.add(new ArrayList<Integer>(list));            return;        }                for(int i = 0; i < num.length; i++) {            if (visited[i] == 1 || (i != 0 && num[i] == num[i - 1] && visited[i - 1] == 0)){                continue;            }            visited[i] = 1;            list.add(num[i]);            helper(result, list, visited, num);            list.remove(list.size() - 1);            visited[i] = 0;        }    }    }




0 0
原创粉丝点击