码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【LeetCode】Day127-四数之和 & 组合总和


    题目1、四数之和

    18. 四数之和【中等】

    题解

    从力扣第一题的两数之和,进阶到三数之和,现在到了四数之和

    仍然可以沿用双指针的方法,先对数组进行排序,然后用两重循环枚举前两个数,双指针枚举后两个数
    还需要注意的一个问题是,四数之和有可能会超出int界限,所以sum要用long型整数表示,其他的就和三数之和一样了

    class Solution {
        public List<List<Integer>> fourSum(int[] nums, int target) {
            List<List<Integer>>res=new ArrayList<>();
            Arrays.sort(nums);
            int n=nums.length;
            if(n<4)
                return res;
            for(int i=0;i<n;i++){
                if(i+3<n&&(long)nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target)//剪枝
                    break;
                for(int j=i+1;j<n;j++){
                    int L=j+1,R=n-1;
                    while(L<R){
                        long sum=(long)nums[i]+nums[j]+nums[L]+nums[R];
                        if(sum==target){
                            res.add(Arrays.asList(nums[i],nums[j],nums[L],nums[R]));
                            while(L+1<R&&nums[L+1]==nums[L])
                                L++;
                            while(R-1>L&&nums[R-1]==nums[R])
                                R--;
                            L++;
                            R--;
                        }
                        else if(sum<target)
                            L++;
                        else
                            R--;
                    }
                    while(j+1<n&&nums[j+1]==nums[j])
                        j++;
                }
                while(i+1<n&&nums[i+1]==nums[i])
                        i++;
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    时间复杂度: O ( n 3 ) O(n^3) O(n3)

    空间复杂度: O ( l o g n ) O(logn) O(logn),排序所需的时间复杂度

    题目2、组合总和

    39. 组合总和【中等】

    题解

    和上一题有点像,不过这题中同一个数字可以被无限次选取,给题目增加了难度

    对于这类寻找所有可行解的题,我们都可以尝试用「搜索回溯」的方法来解决。

    class Solution {
        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            List<List<Integer>>res=new ArrayList<>();
            List<Integer>combine=new ArrayList<>();
            dfs(candidates,target,0,res,combine);
            return res;
        }
        public void dfs(int[] candidates,int target,int idx,List<List<Integer>>res,List<Integer>combine){
            if(idx==candidates.length)
                return;
            if(target==0){
                res.add(new ArrayList<Integer>(combine));
                return;
            }
            //跳过
            dfs(candidates,target,idx+1,res,combine);
            //选择该数
            if(target-candidates[idx]>=0){
                combine.add(candidates[idx]);
                //由于每一个元素可以重复使用,下一轮搜索的起点依然是idx
                dfs(candidates,target-candidates[idx],idx,res,combine);
                combine.remove(combine.size()-1);//回溯
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    时间复杂度: O ( S ) O(S) O(S),其中 S 为所有可行解的长度之和。

    空间复杂度: O ( t a r g e t ) O(target) O(target),递归栈的深度,最深为target层。

  • 相关阅读:
    Rust使用Rust For Windows调用CreateProcessA
    单例模式学习笔记
    html从零开始10:注释与常见输出方式,数据类型,typeof运算符,运算符之算术、赋值、比较、布尔运算符【搬代码】
    win10系统单独编译和使用WebRTC的回声消除(AEC)、音频增益(AGC)、去噪(NS)模块
    数字孪生在灌区信息中的应用
    token、cookie、session的对比以及Java实现
    这种动态规划你见过吗——状态机动态规划之股票问题(下)
    IPTABLES问题:DNAT下如何解决内网访问内部服务器问题
    Mongodb
    d3dx9_42.dll丢失怎么解决?有什么方法解决d3dx9_42.dll丢失问题
  • 原文地址:https://blog.csdn.net/qq_43417265/article/details/126685990
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号