LeetCode Backtracking 46 Permutations 47 Permutationsll

46. Permutations

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

Example:

Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

solution

由于是要得到所有的排列,所以考虑使用DFS (backtracking),同时和子集不同的是,所有排列的长度都相等,只有位置发生了变化,所以backtracking function应该对数组中的数字的位置进行操作,于是就是采用swap method

定义backtracking function,helper(ArrayList nums, List<List> result, int first),
定义是:得到以当前数组nums为起点,从first开始交换数字位置的所有permutations,然后把这些放到result里面。就是nums[first]和nms[furst], nums[first+1]…nums[n]进行swap。

重点是由于nums是在不断变化的所以把他添加到result里面时不可以直接添加,而是要
r e s u l t . a d d ( n e w A r r a y L i s t < I n t e g e r > ( n u m s ) ) ; result.add(new ArrayList<Integer>(nums)); result.add(newArrayList<Integer>(nums));
同时只有当 f i r s t = = n first == n first==n时,才可以添加。

backtracking算法

  1. If the first integer to consider has index n that means that the current permutation is done.
  2. Iterate over the integers from index first to index n - 1.
    2.1 Place i-th integer first in the permutation, i.e. swap(nums[first], nums[i]).
    2.2 Proceed to create all permutations which starts from i-th integer : backtrack(first + 1).
    2.3 Now backtrack, i.e. swap(nums[first], nums[i]) back.
class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return result;
        }
        
        ArrayList<Integer> num = new ArrayList<>();
        for (int tmp : nums) {
            num.add(tmp);
        }
        
        // start from first = 0, and add all the permutation to result
        helper(num, result, 0);
        
        return result;
    }
    
    public void helper(ArrayList<Integer> nums, List<List<Integer>> result, int first) {
        if (first == nums.size()) {
            result.add(new ArrayList<Integer>(nums));
        }
        
        for (int i = first; i < nums.size(); i++) {
            Collections.swap(nums, first, i);
            helper(nums, result, first+1);
            Collections.swap(nums, first, i);
        }
    }
}

47. Permutations II

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

Example:

Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
]

solution

这道题的频率并不高,所以采用一个比较笨的方法解答就足够了,按照上一题的做法加入duplicates会使得结果也出现duplicates,最直接的想法,HashSet可以避免duplicates,所以把结果先存在HashSet里面,最后放到result里面

同时在helper里面加入一个if语句可以减少一些duplicates,主要就是减少了相邻duplicates所产生的结果duplicates,如[1,1,2],但是对于[1,1,2,2]并不能去掉所有duplicates,比如 [ 2 1 , 1 2 , 1 1 , 2 2 ] [2_1,1_2,1_1,2_2] [21,12,11,22] [ 2 2 , 1 2 , 1 1 , 2 2 ] [2_2,1_2,1_1,2_2] [22,12,11,22],所以还是需要HashSet.

class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return result;
        }
        
        ArrayList<Integer> num = new ArrayList<>();
        Arrays.sort(nums);
        for (int tmp : nums) {
            num.add(tmp);
        }
        
        HashSet<ArrayList<Integer>> res = new HashSet<>();
        // start from first = 0, and add all the permutation to result
        helper(num, res, 0);
        
        for (ArrayList<Integer> tmp : res) {
            result.add(tmp);
        }
        
        return result;
    }
    
        public void helper(ArrayList<Integer> nums, HashSet<ArrayList<Integer>> result, int first) {
        if (first == nums.size()) {
            result.add(new ArrayList<Integer>(nums));
        }
        
        for (int i = first; i < nums.size(); i++) {
            if (i != first && nums.get(i) == nums.get(first)) {
                continue;
            }
            Collections.swap(nums, first, i);
            helper(nums, result, first+1);
            Collections.swap(nums, first, i);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值