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 ...
随机推荐
- 洛谷—— P1680 奇怪的分组
https://www.luogu.org/problemnew/show/1680 题目背景 终于解出了dm同学的难题,dm同学同意帮v神联络.可dm同学有个习惯,就是联络同学的时候喜欢分组联络,而 ...
- Kali Linux Wine32英文字体不显示问题
Kali Linux Wine32英文字体不显示问题 Kali Linux提供了Wine32工具.在运行Wine32后,界面可以显示中文,但不能显示英文文字.英文文字均显示为方块.这是由于缺少对应的 ...
- Beginning iOS 8 Programming with Swift-TableView
UITableView控件使用 使用UITableView,在控件库中,拖拽一个Table View到ViewController中,在Controller的后台代码中需要继承UITableViewD ...
- Flatten 2D Vector -- LeetCode
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [,], [], [,,] ] By cal ...
- 【bzoj1123】【[POI2008]BLO】tarjan判割点
(上不了p站我要死了,侵权度娘背锅) Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有t ...
- 线程同步工具之CountDownLatch
CountDownLatch,一个同步辅助类,在完成一组其他线程汇总执行的操作前,它允许一个或多个线程一直等待 主要方法: public CountDownLatch(int count); ...
- iOS开发 Swift开发数独游戏(二)数独题目的生成
一.Plist文件结构设计 由于要预先生成数独题目的文件,我自然而然想到用plist存取. 我用Xcode建了几个plist文件来熟悉这种文件使用的结构后设计了如下结构: 为区分难度(后来了解到挖空数 ...
- linux:ping不通www.baidu.com
如果某台Linux服务器ping不通域名, 如下提示: [root@localhost ~]# ping www.baidu.com ping: unknown host www.baidu.com ...
- WEB接口测试之Jmeter接口测试自动化 (二)(数据分离)
转载: http://www.cnblogs.com/chengtch/p/6105231.html 通过逐个录入的方式,好不容易将需要测试几十个接口的300多个测试用例录入sampler-ht ...
- C++003基础
1.C++对C的扩展 1简单的C++程序 1.1求圆的周长和面积 数据描写叙述: 半径.周长,面积均用实型数表示 数据处理: 输入半径 r. 计算周长 = 2*π*r : 计算面积 = π* r2 . ...