第29天 Backtracking 491、46、47

491. Increasing Subsequences 

  • 难点:去重。类似90. Subsets ii,但区别在于,subsets ii是可以sort的,这道题不行。
  • 但是同样去重都是对于同一父节点下的同层上使用过的元素就不能在使用了

  •  用boolean[] used 记录这个nums[i]的元素在本层用过了,这里的used和subsets ii里的used不同
    • 这里的used:每一层
    • subsets ii里的used: 全局变量
  • 注意这里判断新的元素比temp里之前有的大或等于,是用!temp.isEmpty() && nums[i] < temp.get(temp.size()-1)
class Solution {
    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        boolean[] used = new boolean[201];
        backtracking(nums, ans, new ArrayList<>(), 0);
        return ans;
    }

    public void backtracking(int[] nums, List<List<Integer>> ans, List<Integer> temp, int start) {
        if (temp.size() >= 2) 
            ans.add(new ArrayList<>(temp));

        boolean[] used = new boolean[201];
        for (int i=start; i<nums.length; i++) {
            if (!temp.isEmpty() && nums[i] < temp.get(temp.size()-1)) continue;
            if (used[nums[i]+100]) continue;
            temp.add(nums[i]);
            used[nums[i]+100] = true;
            backtracking(nums, ans, temp, i+1);
            temp.remove(temp.size()-1);
        }
    }
}

46. Permutations

  • 没有startIndex, 排列问题每次都要从头开始搜索,例如元素1在[1,2]中已经使用过了,但是在[2,1]中还要再使用一次1。
  • Time Complexity: O(N * N!) 
    • when adding the list to the result list, it takes O(N)
    • Number of solutions: N!
  • Space Complexity: O(N) - recursion depth
class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        backtracking(nums, ans, new ArrayList<>());
        return ans;
    }

    public void backtracking(int[] nums, List<List<Integer>> ans, List<Integer> temp) {
        if (temp.size() == nums.length) {
            ans.add(new ArrayList<>(temp));
            return;
        }
        for (int i=0; i<nums.length; i++) {
            if (temp.contains(nums[i])) continue;
            temp.add(nums[i]);
            backtracking(nums, ans, temp);
            temp.remove(temp.size()-1);
        }
    }
}
  • 保证每一个temp内没有重复的元素:可以用temp.contains(nums[i]),也可以用used[]

class Solution {
    List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
    LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
    boolean[] used;
    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0){
            return result;
        }
        used = new boolean[nums.length];
        permuteHelper(nums);
        return result;
    }

    private void permuteHelper(int[] nums){
        if (path.size() == nums.length){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++){
            if (used[i]){
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            permuteHelper(nums);
            path.removeLast();
            used[i] = false;
        }
    }
}

47. Permutations ii

  • 方法1:
    • 用sorting,和全局变量used,同时表示同一树层和同一树枝上有没有重复。
    • 注意used[i - 1] == false也可以(树层去重),used[i - 1] == true也可以(树枝去重

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> permuteUnique(int[] nums) {
        boolean[] used = new boolean[nums.length];
        Arrays.fill(used, false);
        Arrays.sort(nums);
        backTrack(nums, used);
        return result;
    }

    private void backTrack(int[] nums, boolean[] used) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            // used[i - 1] == true,说明同⼀树枝nums[i - 1]使⽤过
            // used[i - 1] == false,说明同⼀树层nums[i - 1]使⽤过
            // 如果同⼀树层nums[i - 1]使⽤过则直接跳过
            if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
                continue;
            }
            //如果同⼀树⽀nums[i]没使⽤过开始处理,否则跳过
            if (used[i] == false) {
                used[i] = true;//标记同⼀树枝nums[i]使⽤过,防止同一树枝重复使用
                path.add(nums[i]);
                backTrack(nums, used);
                path.remove(path.size() - 1);
                used[i] = false;
            }
        }
    }
}
  • 方法2:
    • check:全局变量,用来表示同一树枝上没有重复。例如,nums=[1,2,3], temp=[1, ], 在进行下一个元素的选择时,用check来跳过1,直接[1,2, ] 
    • used:每一层,用来表示同一树层上没有重复。例如,nums=[1,1,2], ans=[[1,2,1], ], 在寻找下一个temp时,直接跳过以第二个1为开头的所有可能性([1,1,2]),跳到以2为开头
  • Time Complexity: O(N * N!) 
    • when adding the list to the result list, it takes O(N)
    • Number of solutions: N!
  • Space Complexity: O(N) - recursion depth
class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        boolean[] check = new boolean[nums.length];
        backtracking(nums, ans, new ArrayList<>(), check);
        return ans;
    }

    public void backtracking(int[] nums, List<List<Integer>> ans, List<Integer> temp, boolean[] check) {
        if (temp.size() == nums.length) {
            ans.add(new ArrayList<>(temp));
            return;
        }
        boolean[] used = new boolean[21];
        for (int i=0; i<nums.length; i++) {
            if (used[nums[i]+10]) continue;
            if (check[i]) continue;
            temp.add(nums[i]);
            used[nums[i]+10] = true;
            check[i] = true;
            backtracking(nums, ans, temp, check);
            check[i] = false;
            temp.remove(temp.size()-1);
        }
    }
}

去重 non-redundancy

  • 树层去重,可以用sorting+全局变量used(针对nums的index)
  • 树层去重,也可以用每一层新建的used(针对nums的element)
  • 树枝去重,可以用!temp.cotains(),对于nums只有distinct elements的情况
  • 树枝去重,也可以用全局变量used(针对nums的index),每次进行回溯used[i]=true; used[i]=false;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值