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 ...
随机推荐
- Codeforces Round #325 (Div. 2) Alena's Schedule 模拟
原题链接:http://codeforces.com/contest/586/problem/A 题意: 大概就是给你个序列..瞎比让你统计统计什么长度 题解: 就瞎比搞搞就好 代码: #includ ...
- 微服务实施Spring Boot/Spring Cloud中踩过的坑(转)
http://tietang.wang/2016/09/08/%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%AE%9E%E6%9 ...
- MSSQL收缩事务日志&日志文件过大无法收缩
原文:MSSQL收缩事务日志&日志文件过大无法收缩 一.MS SQL SERVER 2005 --1.清空日志 exec('DUMP TRANSACTION 数据库名 WITH NO_LOG' ...
- lrc 校验码 ascii 格式
lrc 校验码 ascii 格式 将adr1 (站号)至最后一个数据内容相加,得到结果以256为单位,超出部分去除(如得到的结果为16#128H则只取28H,) 然后计算二次反补得到后的结果即为侦误 ...
- 模拟Spring中的getBean方法
一直知道Spring是运用反射技术的,但具体怎么用呢?今天就模拟下getBean方法. 步骤: 1.用Dom4j解析xml配置文件,取出我们需要的信息 2.遍历Bean节点,根据每个Bean节点的cl ...
- 怎样设计REST中间件---中间件JSON对数据库数据的组织
怎样设计REST中间件---中间件JSON对数据库数据的组织 SQL查询语句有:select SQL非查询语句有:insert, update, delete 三种 中间件JSON对数据库数据的组织也 ...
- 基于Rebound制造绚丽的动画效果-入门篇
基于Rebound制造绚丽的动画效果-入门篇 Rebound是什么? Rebound是一个来自 Facebook 公司的 Java物理和动画库.Rebound spring 模型可用于创建动画,让你感 ...
- ansible的inventory文件含义
默认文件为/etc/ansible/hosts 例如 [test] web.yinzhipeng.com dhcp ansible_ssh_host=172.16.18.195 1.中括号中的名字代表 ...
- docker部署 自动测试脚本记录
1 首先配置virtualenv, 并启动之,source py3env/bin/activate 2 安装各种必备包 ,如simplejson,chardet, configparser等 3 注意 ...
- 倍福TwinCAT(贝福Beckhoff)基础教程 松下伺服驱动器报错 24.0怎么办
24.0 位置偏差过大保护 读取驱动器参数之后,在基本的014项目把设定值设置为最大,然后点击传送,EEP写入驱动器后重启驱动器即可 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空 ...