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. Out of Sorts II

    问题 E: Out of Sorts II 时间限制: 1 Sec  内存限制: 128 MB提交: 68  解决: 4[提交] [状态] [讨论版] [命题人:] 题目描述 Keeping an e ...

  2. c#作业(2班)

    第二章 1.编写一个控制台程序,要求: 接受从控制台输入的姓名,如:张三 程序响应:你好,张三. 在代码中使用规范的注释,说明程序的功能 编译程序,并执行. 程序执行效果如下图: using Syst ...

  3. redis 安装并且设置开机后台自动启动(转)

      1,安装redis wget http://download.redis.io/releases/redis-2.8.8.tar.gz .tar.gz cd redis- make 2,建立Red ...

  4. MySQL索引,MySQL性能分析及explain的使用,分析SQL查询性能

    可以使用explain来分析MySQL查询性能,举例如下: 1.使用explain语句去查看分析结果 如 explain select * from test1 where id=1; 会出现: id ...

  5. IE8下 input标签内padding失效

    在做网页兼容时 发现在ie8下的input内用padding失效 为了达到居中文字的效果 使用line-height可以解决问题

  6. java内存缓存,节省内存

    缓存的对象 这个问题就是我们上面提到的极端情况,在Java中,会对-128到127的Integer对象进行缓存,当创建新的Integer对象时,如果符合这个这个范围,并且已有存在的相同值的对象,则返回 ...

  7. ios学习流水账2

    1.UISearchBar自定义背景.取消按钮中文设置 UISearchBar *seachBar=[[UISearchBar alloc] init]; //修改搜索框背景 seachBar.bac ...

  8. cordova百度导航插件使用

    org.ssgroup.sope.cordova.baiduNavi 插件已经开源至 https://github.com/shenshouer/org.ssgroup.sope.cordova.ba ...

  9. 浅谈Java中静态初始化块跟非初始化块

    众所周知在JAVA编程语言中有两种初始化块:   静态初始化块 非静态初始化块 他们到底有什么区别呢?今天就浅谈一下JAVA中静态初始化块和非静态初始化块的区别   静态初始化块 定义:       ...

  10. 2017.4.7 java异常处理总结

    目录 1.java异常处理的几种错误做法 2.异常处理示例 3.常用异常 4.异常类的继承关系 5.异常处理机制 6.Throw和Throws的区别 7.e.toString(), e.getCaus ...