DAY25

非递减子序列

    List<List<Integer>> res=new ArrayList<List<Integer>>();
    LinkedList<Integer> path=new LinkedList<>();

    public List<List<Integer>> findSubsequences(int[] nums) {
        trackBack(nums,0);
        return res;

    }
    public void trackBack(int[] nums,int index){
        if(path.size()>=2){
            res.add(new ArrayList<>(path));
        }
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i=index;i<nums.length;i++){
           if(!path.isEmpty()&&path.getLast()>nums[i])continue;
           if(map.getOrDefault(nums[i],0)>=1)continue;
            map.put(nums[i],map.getOrDefault(nums[i],0)+1);
            path.add(nums[i]);
            trackBack(nums,i+1);
            path.removeLast();
            }

        }

全排列

    List<List<Integer>> res=new ArrayList<List<Integer>>();
    LinkedList<Integer> path=new LinkedList<Integer>();
    boolean []used;
    public List<List<Integer>> permute(int[] nums) {
        used=new boolean[nums.length];
        trackBack(nums);
        return res;


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

    }

全排列 II

    List<List<Integer>> res=new ArrayList<List<Integer>>();
    LinkedList<Integer> path=new LinkedList<Integer>();
    boolean[] used;
    public List<List<Integer>> permuteUnique(int[] nums) {
        used=new boolean[nums.length];
        Arrays.sort(nums);
        trackback(nums);
        return res;


    }
    public void trackback(int[]nums){
        if(path.size()==nums.length){
            res.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;
                trackback(nums);
                path.removeLast();
                used[i]=false;
            }
        }
    }

重新安排行程

    LinkedList<String> res;
    LinkedList<String>path=new LinkedList<>();
    public List<String> findItinerary(List<List<String>> tickets) {
        Collections.sort(tickets,(a,b)->a.get(1).compareTo(b.get(1)));
        path.add("JFK");
        boolean []used=new boolean[tickets.size()];
        backTrack((ArrayList)tickets,used);
        return res;
        

    }
    public boolean backTrack(ArrayList<List<String>>tickets,boolean[]used){
        if(path.size()==tickets.size()+1){
            res=new LinkedList(path);
            return true;
        }
        for(int i=0;i<tickets.size();i++){
            if(!used[i]&&tickets.get(i).get(0).equals(path.getLast())){
                path.add(tickets.get(i).get(1));
                used[i]=true;
                if(backTrack(tickets,used)){
                return true;
                }
                used[i]=false;
                path.removeLast();
            }
            
        }
        return false;
    }

超过了时间限制

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值