面试经典150题(85-87)

leetcode 150道题 计划花两个月时候刷完,今天(第四十三天)完成了3道(85-87)150:

85.(77. 组合)题目描述:

bash 复制代码
给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。
你可以按 任何顺序 返回答案。
输入:n = 4, k = 2
输出:
[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],]

第一版(昨天就是这个卡了好久没弄出来,今天还是没思路啊。。看了解题,感觉都是一个for 然后for里面嵌套。看看解题的代码吧)

java 复制代码
class Solution {
    List<List<Integer>> res=new ArrayList();
    public List<List<Integer>> combine(int n, int k) {
        if(n<k){
            return res;
        }
        selectKNum(n,k,1,new ArrayList<Integer>());
        return res;
    }
    public void selectKNum(int n, int k,int start,List<Integer> list) {
        if(list.size()==k){
            res.add(new ArrayList(list));
            return ;
        }
        for(int i=start;i<=n;i++){
            list.add(i);
            selectKNum(n,k,i+1,list);
            list.remove(list.size()-1);
        }
        
    }
}

86.(46. 全排列)题目描述:

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

第一版(还是解题,感觉这个又是另一种回溯的题型)

java 复制代码
class Solution {
    List<List<Integer>> res=new ArrayList();
    public List<List<Integer>> permute(int[] nums) {
        boolean[] used = new boolean[nums.length];
        permuteCore(nums,used,new ArrayList<Integer>());
        return res;
    }
    public void permuteCore(int[] nums,boolean[] used,List<Integer> list){
        if(list.size()==nums.length){
            res.add(new ArrayList(list));
            return ;
        }
        for(int i=0;i<nums.length;i++){
            if(used[i]){
                continue;
            }
            list.add(nums[i]);
            used[i]=true;
            permuteCore(nums,used,list);
            used[i]=false;
            list.remove(list.size()-1);
        }
    }
}

87.(39. 组合总和)题目描述:

bash 复制代码
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。 
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
candidates 全是大于0的整数
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]

第一版(根据之前的两个题的解题,照猫画虎写了一版。。终于自己写了一版了,虽然性能不是很好)

java 复制代码
class Solution {
    List<List<Integer>> res=new ArrayList();
    Set<String> set=new HashSet();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        combinationSumCore(candidates,target,new ArrayList<Integer>());
        return res;
    }
    public void combinationSumCore(int[] candidates,int target,List<Integer> list){
        if(0==target){
            List<Integer> temp=new ArrayList(list);
            temp.sort((i1,i2)->{return i1-i2;});
            if(set.add(temp.toString()))
            {
                res.add(temp);
            }
            return ;
        }
        for(int i=0;i<candidates.length;i++){
            if(target<0){
                break;
            }
            if(target-candidates[i]<0){
                continue;
            }
            list.add(candidates[i]);
            combinationSumCore(candidates,target-candidates[i],list);
            list.remove(list.size()-1);
        }
    }
}

第二版(看了解题,讲的还是理解不了,就照着写了一版,真的是性能从 104ms->5ms 质的飞跃)

java 复制代码
class Solution {
    List<List<Integer>> res=new ArrayList();
    Set<String> set=new HashSet();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        combinationSumCore(candidates,0,target,new ArrayList<Integer>());
        return res;
    }
    public void combinationSumCore(int[] candidates,int start,int target,List<Integer> list){
        if(0==target){
            res.add(new ArrayList(list));
            return ;
        }
        for(int i=start;i<candidates.length;i++){
            if(target<0){
                break;
            }
            if(target-candidates[i]<0){
                continue;
            }
            list.add(candidates[i]);
            combinationSumCore(candidates,i,target-candidates[i],list);
            list.remove(list.size()-1);
        }
    }
}

就这两天回溯我感觉咋都是 for 加递归调用。。但是不好理解。。不行就背下来(笨人做法)

加油,第四十三天了,早日跳槽啊!!!

相关推荐
Coovally AI模型快速验证2 小时前
农田扫描提速37%!基于检测置信度的无人机“智能抽查”路径规划,Coovally一键加速模型落地
深度学习·算法·yolo·计算机视觉·transformer·无人机
pusue_the_sun2 小时前
数据结构:二叉树oj练习
c语言·数据结构·算法·二叉树
RaymondZhao343 小时前
【全面推导】策略梯度算法:公式、偏差方差与进化
人工智能·深度学习·算法·机器学习·chatgpt
艾伦~耶格尔3 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
zhangfeng11333 小时前
DBSCAN算法详解和参数优化,基于密度的空间聚类算法,特别擅长处理不规则形状的聚类和噪声数据
算法·机器学习·聚类
一只叫煤球的猫3 小时前
🕰 一个案例带你彻底搞懂延迟双删
java·后端·面试
圣保罗的大教堂4 小时前
leetcode 2348. 全 0 子数组的数目 中等
leetcode
啊阿狸不会拉杆4 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路4 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
你知道网上冲浪吗5 小时前
【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架
python·算法·数学建模·数值分析