package com.jay.leetcode.p_47_permutations;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by zhoujie on 2018/04/29
 */
public class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();

        backList(result, new ArrayList<>(), new HashSet<>(), nums);

        return result;
    }
    private void backList(List<List<Integer>> result, List<Integer> tmpList, Set<Integer> added, int[] nums) {
        if (tmpList.size() == nums.length) {
            result.add(new ArrayList<>(tmpList));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!added.add(i)) {
                continue;
            }
            tmpList.add(nums[i]);
            backList(result, tmpList, added, nums);
            tmpList.remove(tmpList.size() - 1);
            added.remove(i);
        }
    }
}
