two sum, three sum和four sum问题
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问题的更多相关文章
- js中sum(2,3,4)和sum(2)(3)(4)都返回9并要求扩展性
网上有很多关于sum(1)(2)(3),sum(1,2,3)之类的面试题要求输出相同的结果6并要求可以满足扩展,即有多个参数时也能符合题设的要求,所以自己写了部分例子可以大概满足这些面试题的要求 &l ...
- 编写一个求和函数sum,使输入sum(2)(3)或输入sum(2,3),输出结果都为5
昨天的笔试题,做的一塌糊涂,题目考的都很基础而且很细,手写代码对我来说是硬伤啊.其中有一道是这个,然而看到题目的时候,根本没有想到arguments:然后现在就恶补一下. arguments:用在函数 ...
- 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 ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 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 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [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 ...
- [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 ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
随机推荐
- C语言基础之指针
1.指针的定义 格式:变量类型 *变量名; 1: // 定义了一个指针变量p 2: // 指针变量只能存储地址 3: // 指针就一个作用:能够根据一个地址值,访问对应的存储空间 4: // 指针变量 ...
- html 基础二
HTMLCSS基础2 一.HTML中的标签 1.1标签的特点 给文本加上含有语义的标签 应该学习更多具体语义标签 标签:用“<>”包起来的内容 2.1 更多的标签 2.1.1 h系列的标签 ...
- Xcode 5 单元测试(二)OCMock和GHUnit
在Xcode 5 单元测试(一)使用XCTest进行单元测试中说了如何在Xcode 5中使用XCTest进行简单的单元测试,本文就来探讨下mock测试和更高级的工具GHUnit. Mock 首先科普下 ...
- 【maven】maven的web项目打包报错:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK
打包过程中报错如下: No compiler is provided in this environment. Perhaps you are running on a JRE rather than ...
- 关联模型中如果condition条件
在练习中,有一个user表和地址表,一对多的关系. 我的想法是,通过这个关联模型找出这个用户下面默认值字段为1的地址 控制器中 public function index(){ $User = D(' ...
- S5PV210之内外存学习
RAM,内部存储器,用来运行程序(DRAM,SRAM,DDR) ROM,外部存储器,存储数据.程序(硬盘,FLASH等) 内存:SRAM,静态内存,容量下,价格高,不需要初始化,上电后直接使用 DRA ...
- 18 Tar Command Examples in Linux
FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ...
- zabbix自动化监控之自动注册
自动注册与自动发现刚好相反,是zabbix agent主动联系zabbix server,最后由zabbix server将这些agent加到host里.活动的Zabbix agent可以自动注册到服 ...
- Node.js node主文件找不到时报出的Error:Cannot find module异常
如果执行>Node xx.js时,xx,js找不到的话,报出的错误是Error:Cannot find module 'c:/test/xx,js' 主文件名打错或者是点号打成逗号都会出这样的错 ...
- 从零单排之玩转Python安全编程(II)
转自:http://www.secpulse.com/archives/35893.html 都说Python大法好,作为一名合格的安全从业人员,不会几门脚本语言都不好意思说自己是从事安全行业的. 而 ...