刷题11.9

1 递增子序列

题目

给你一个整数数组 nums ,找出并返回所有该数组中不同的递增子序列,递增子序列中 至少有两个元素 。你可以按 任意顺序 返回答案。
数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。
示例 1:
输入:nums = [4,6,7,7]
输出:[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
示例 2:
输入:nums = [4,4,3,2,1]
输出:[[4,4]]

代码

class Solution {
    List<List<Integer>> result =new ArrayList<>();
    List<Integer>path =new ArrayList<>();
    public List<List<Integer>> findSubsequences(int[] nums) {
        backtracking(nums,0);
        return result;

    }
    public void backtracking(int[] nums,int start){
        if(path.size()>1){
            result.add(new ArrayList<>(path));
        }
        int[] used = new int[201];
        for(int i=start;i<nums.length;i++){
            if(!path.isEmpty() && nums[i]<path.get(path.size()-1) ) continue;
            if(used[nums[i] + 100] == 1) continue;
            used[nums[i] + 100] = 1;
            path.add(nums[i]);
            backtracking(nums,i+1);
            path.remove(path.size()-1);
        }
    }
}

总结

示例1给的很有歧义性,让人感觉仿佛示例2答案不对。我很困惑,都看不懂题目意思。看了卡哥解析,原来本题求自增子序列,是不能对原数组经行排序的,排完序的数组都是自增子序列了。
本题的另一个难点就是去重,因为数据不一定都是递增的,所以不能用之前那个nums[i-1]==nums[i]来判断了。用了哈希,厉害。映射数组更省时,牛啊!

*2 全排列

题目

给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
示例 1:
输入:nums = [1,2,3]
输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

代码

class Solution {
    List<List<Integer>> result =new ArrayList<>();
    List<Integer>path =new ArrayList<>();
    boolean[] used;
    public List<List<Integer>> permute(int[] nums) {
        if(nums.length==0) return result;
        used=new boolean[nums.length];
        backtracking(nums);
        return result;
    }
    public void backtracking(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]);
            backtracking(nums);
            path.remove(path.size()-1);
            used[i]=false;
        }
    }
}

总结

和之前做的有点不一样,这一题不需要标记start。不标记开始位置,但是却需要继续元素有没有使用过,其实这个整体回溯逻辑,我没理解。我觉得需要debug一下,看看是怎么运行的。

3 全排列II

题目

给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
示例 1:
输入:nums = [1,1,2]
输出:
[[1,1,2],
[1,2,1],
[2,1,1]]

代码

class Solution {
    List<List<Integer>> result =new ArrayList<>();
    List<Integer>path =new ArrayList<>();
    boolean[] used;
    public List<List<Integer>> permuteUnique(int[] nums) {
        if(nums.length==0) return result;
        used=new boolean[nums.length];
        Arrays.sort(nums);
        backtracking(nums);
        return result;
    }
    public void backtracking(int[] nums){
        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]) continue;
            used[i]=true;
            path.add(nums[i]);
            backtracking(nums);
            path.remove(path.size()-1);
            used[i]=false;
        }
    }
}

总结

虽然只是多了去重这一步,但是去重又有点不一样啊。
这个used[i-1]还有得好好想想。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值