1. two sum问题

给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的一对数

简单做法:两层循环遍历,时间复杂度为n^2

升级版:对给定的序列建立一个hash表,然后只需要外层一层循环就可以了,时间复杂度为n

2. three sum问题

给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的三个数

简单做法:三层for循环,时间复杂度为n^3

升级版:

  如果看做是two sum升级,可以先从小到大排序,时间复杂度nlogn,然后固定一个数,前面的子列使用two sum的方法,

时间复杂度为n^2;

  也可以先排序,使用两层for循环,遍历子列,获取前两个数,然后对后面的hash过的子列找出另一个,时间复杂度为n^2;

3. four sum问题

可以类比three sum问题,时间复杂度可达到n^3

上述问题我就只能想到这种解法了,但是我感觉对时间复杂度还是不满意

two sum, three sum和four sum问题的更多相关文章

  1. js中sum(2,3,4)和sum(2)(3)(4)都返回9并要求扩展性

    网上有很多关于sum(1)(2)(3),sum(1,2,3)之类的面试题要求输出相同的结果6并要求可以满足扩展,即有多个参数时也能符合题设的要求,所以自己写了部分例子可以大概满足这些面试题的要求 &l ...

  2. 编写一个求和函数sum,使输入sum(2)(3)或输入sum(2,3),输出结果都为5

    昨天的笔试题,做的一塌糊涂,题目考的都很基础而且很细,手写代码对我来说是硬伤啊.其中有一道是这个,然而看到题目的时候,根本没有想到arguments:然后现在就恶补一下. arguments:用在函数 ...

  3. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  4. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  5. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  8. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  9. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  10. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

随机推荐

  1. [BZOJ4398]福慧双修/[BZOJ2407]探险

    题目大意: 给定一个$n(n\leq40000)$个点$m(m\leq100000)$条边的有向图,求从$1$出发回到$1$的不经过重复结点的最短路. 思路: 首先Dijkstra求出从1出发到每个结 ...

  2. MySQL区间统计SQL

    SELECT elt( INTERVAL ( datediff(END_DATE, CURDATE()), 1, 201, 401, 601 ), '0-200', '200-400', '400-6 ...

  3. ostu进行遥感图像的分割

    城市地区道路网的简单的阈值分割.采用的是单ostu(最佳阈值分割)算法,废话少说,如果不太清楚该算法,请参考文献[1]中的图像分割这一章的介绍.程序直接运行的效果如下.

  4. ASIHTTPRequest框架使用总结系列之阿堂教程5(上传数据)

    在上篇文章中,阿堂和网友们分享了如何用ASIHTTPRequest框架下载数据的实例,本篇阿堂将数据介绍如何用ASIHTTPRequest框架上传数据的应用实例.       数据上传是通过ASIHT ...

  5. 项目笔记:导出Excel功能分sheet页插入数据

    导出Excel功能分sheet页处理数据: /*导出EXCEL*/ public void createExcel() { log.info("导出Excel功能已经启动-BEGIN&quo ...

  6. SparkMLlib分类算法之逻辑回归算法

    SparkMLlib分类算法之逻辑回归算法 (一),逻辑回归算法的概念(参考网址:http://blog.csdn.net/sinat_33761963/article/details/5169383 ...

  7. css 文字超出省略号

    white-space:nowrap; overflow:hidden; -o-text-overflow:ellipsis; text-overflow:ellipsis; 语法: text-ove ...

  8. Linux学习之三-Linux系统的一些重要配置文件

    Linux学习之三-Linux系统的一些重要配置文件 1.网卡配置文件 /etc/sysconfig/network-scripts/ifcfg-eth0 说明: DEVICE=eth0        ...

  9. Asp.net问题集锦

    1.在Web应用开发中经常碰到这样的情况,Dropdownlist绑定的数据太多,用户要选择某一项必须从头找到尾,使用起来很不方便.最近我在工作中就碰到这种情况,公司内某个业务系统需要绑定几百条的厂家 ...

  10. LeetCode 14: Longest Common Prefix

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...