第七章 回溯算法part05

491.递增子序列

第一次没咋看思路 一遍过的medium 记录下!!

重点是知道树层中不能有重复的,所以每一个递归中需要个HashMap记录用没用过一个元素

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        if(nums.length == 0 || nums == null)return result;
        backtrack(nums,0);
        return result;


    }
    private void backtrack(int[] nums, int startIndex){
        Map<Integer,Integer> map = new HashMap<>();

        if(path.size() >= 2){
            result.add(new ArrayList<>(path));
        }
        for(int i = startIndex; i < nums.length; i++){
            Integer value = map.get(nums[i]);
            if((path.size()>0 && path.get(path.size()-1) > nums[i]) || (value != null && value > 0)){
                continue;
            }else{
                path.add(nums[i]);
                map.put(nums[i],map.getOrDefault(nums[i],0)+1);
                backtrack(nums,i+1);
                path.removeLast();
            }
        }
    }
}

46.全排列

没看代码随想录自己写的,效率有点低 时间复杂度高了

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

    public List<List<Integer>> permute(int[] nums) {
        if(nums.length == 0 || nums == null)return result;
        backtrack(nums);
        return result;

    }
    private void backtrack(int[] nums){
        if(path.size() == nums.length){
            result.add(new ArrayList<>(path));
        }
        for(int i = 0; i < nums.length; i++){
            int exist = 0;
            for(int eachone : path){
                if(nums[i] == eachone){
                    exist = 1;
                }
            }
            if(exist == 1)continue;
            path.add(nums[i]);
            backtrack(nums);
            path.removeLast();
        }
    }
}

47.全排列 II

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        if(nums.length == 0 || nums == null)return result;
        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++){
            if(i > 0 && nums[i] == nums[i-1] && used[i-1] == false)continue;
            if(used[i] == false){
                path.add(nums[i]);
                used[i] = true;
                backtrack(nums,used);
                path.removeLast();
                used[i] = false;
            }
            
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值