算是经典算法问题了。这里主要针对只存在一个解或者只需要求一个解的情况描述一下解题思路。若需要找到所有可能解,方法需要略作调整。如有问题,欢迎指正。

2 sum:

如果已排序,可直接用夹逼法,即两指针从头尾向中间移动,使和靠近target。时间复杂度为O(n)。

若未排序,因为除非有特别限制,比较排序的时间复杂度为O(nlgn)。此时用hash更为合适,可以达到O(n)的时间复杂度。此方法实际上对已排序数组也实用。步骤如下:

每一个数a在放入hash表前,判断目标target-a是否在hash 表内,如果在,则找到解。否则将a放入表中。

这种方法比先完整构建hash表再检查少扫描一遍数组。

k sum(k>=3):

  一个通用的方法就是先排序,然后依次固定序列中的一个数,对其后的所有数递归做k-1 sum。这样的时间复杂度是O(nk-1)。

4 Sum:

除了上面k sum的方法外,另一个方法是降到k/2=2 sum。方法大致如下:

把原数组的元素两两求和(非相同序号,个数为O(n2)),记录在一个结构体单元内,其中包含此和以及对应两元素的原始index,时空复杂度均为O(n2)

以两两和为关键字进行排序,时间复杂度O(n2logn)

再用夹逼求2 sum,若两值包含某相同下标(最多四次比较)则跳过,指针按上次方向继续移动。复杂度O(n2)。

所以总的复杂度为O(n2logn)。同样,若最后的夹逼若改为hash,可到O(n2)。

理论上讲,这种方法也许也可以推广到更高价k=2m sum,但是这是一种空间换时间的方法,空间增长很快,而且因为涉及判断下标是否重复,逻辑真的有点复杂。。。

3-Sum Closest

与3 sum类似,但固定一个数后,用夹逼时,记录两个数的和与当前target的差。时间复杂度仍然为O(n2)。

若需要找到所有可能解且元素有重复时,使用上述通用方法,在做递归的过程中,若当前求k sum,重复元素个数为m,则根据和中包含0~min(k,m)个此元素继续递归即可。

K Sum(2 Sum,3 Sum,4 Sum,3-Sum Closest)的更多相关文章

  1. [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)

    Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...

  2. 实现一个函数功能:sum(1,2,3,4..n)转化为 sum(1)(2)(3)(4)…(n)?

    // 使用柯里化 + 递归function curry ( fn ) {  var c = (...arg) => (fn.length === arg.length) ?           ...

  3. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  4. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  5. Leetcode: Max Sum of Rectangle No Larger Than K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  6. Max Sum of Rectangle No Larger Than K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  7. [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

  8. [LeetCode] Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  9. [Swift]LeetCode698. 划分为k个相等的子集 | Partition to K Equal Sum Subsets

    Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...

  10. [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

随机推荐

  1. CentOS7安装MySQL8.0小计

    之前讲配置文件和权限的时候有很多MySQL8的知识,有同志说安装不太一样,希望发个文,我这边简单演示一下 1.环境安装 下载MySQL提供的CentOS7的yum源 官方文档:<https:// ...

  2. scala actor编程之对象传递

    scala 最吸引人的一点就是actor并发编程了.但是纵观scala官方文档,baidu文档,IBM文档都写的通过字符串传呀传,如果用作actor编程说明当然没有问题.但是在正式开放中,光传字符串就 ...

  3. R语言图形base系统(三)

     本篇介绍R语言base系统绘制散点图.条形图.直方图.箱线图.饼图,还将简单介绍点图.核密度图.折线图. 散点图: attach(mtcars) plot(wt, mpg, main="B ...

  4. php......调研投票练习

    调研题目与调研选项显示页面<style type="text/css"> #list{ width:400px; height:200px;} #jieguo{ wid ...

  5. Java多线程系列 JUC线程池07 线程池原理解析(六)

     关闭“线程池” shutdown()的源码如下: public void shutdown() { final ReentrantLock mainLock = this.mainLock; // ...

  6. [原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  7. 【Flask】Sqlalchemy 常用数据类型

    ### SQLAlchemy常用数据类型:1. Integer:整形,映射到数据库中是int类型.2. Float:浮点类型,映射到数据库中是float类型.他占据的32位.3. Double:双精度 ...

  8. vue backup

    为了解决跨域问题 目前在项目,目录下创建了vue.config.js文件,目前是指向别外的一个端口,之前是记得在express后端进行端口指定的,先备注一下,后期跟进 npm ls | grep ax ...

  9. linux 下载rpm包到本地,createrepo:创建本地YUM源

    如何下载rpm包到本地 设置yum安装时,保留rpm包. 1.编辑 /etc/yum.conf 将keepcache的值设置为1; 这样就可以将yum安装时的rpm包保存在 /var/cache/yu ...

  10. Sobel边缘检测

    Sobel算子:[-1  0  1 -2  0  2 -1  0  1] 用此算子与原图像做卷积,可以检测出垂直方向的边缘.算子作用在图像的第二列,结果是:200,200,200:作用在第三列,结果是 ...