K Sum(2 Sum,3 Sum,4 Sum,3-Sum Closest)
算是经典算法问题了。这里主要针对只存在一个解或者只需要求一个解的情况描述一下解题思路。若需要找到所有可能解,方法需要略作调整。如有问题,欢迎指正。
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)的更多相关文章
- [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 ...
- 实现一个函数功能:sum(1,2,3,4..n)转化为 sum(1)(2)(3)(4)…(n)?
// 使用柯里化 + 递归function curry ( fn ) { var c = (...arg) => (fn.length === arg.length) ? ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- ubuntun下安装Fiddler
对于分析网页或者写爬虫的时候经常需要用到抓包工具进行网页数据的抓包.在Windows下可以安装Fiddler来抓包.在ubuntun下不能直接安装Fiddler.需要先安装mono 1 首先安装mon ...
- linux c编程:进程控制(一)
一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程, 也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同 ...
- Jquery 动态添加option 并给option添加数据,并帮定事件
//添加下拉框 function append_add(data) { $('#add').append( '<div class="form-group" id='+for ...
- Java之线程池(二)
关于线程和线程池的学习,我们可以从以下几个方面入手: 第一,什么是线程,线程和进程的区别是什么 第二,线程中的基本概念,线程的生命周期 第三,单线程和多线程 第四,线程池的原理解析 第五,常见的几种线 ...
- vim复制多行
比如我要复制从第1行到第5行的数据,复制到第9行 光标移到第5行任意位置,输入ma光标移到第1行任意位置,输入y'a(这一定要打这个“'”单引号,否则就进入“INSERT”状态了光标移到需要复制的行, ...
- SpringBoot学习笔记(3):静态资源处理
SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...
- linux shell 基础 使用日志与心得
linux shell 基础 使用日志与心得 1.#!/bin/bash 第一行就出现#!/bin/bash是指此脚本使用/bin/bash来解释执行.其中,#!是一个特殊的表示符,其后,跟着解释此脚 ...
- 模仿jquery框架源码---网络
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Page& ...
- iOS category 类别 和 extension 扩展
category 类别 又称为 分类 在ios项目开发中允许使用类别为现有的类添加新的方法,并不需要创建子类.通过类别我们可以动态地为现有的类添加新的方法,可以将类的定义模块化地布局到多个相关文件中 ...
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-tha ...