the sum of two fixed value
the sum of two fixed value
description
Input an array and an integer, fina a pair of number in the array so that the sum is equals to the inputed integer. If there are several pairs, you can output any pair. For example, if the input array is [1,2,4,5,7,11,15] and an integer 15, because 4 + 11 = 15, hence output 4 and 11.
analysis and solution
We try to figure out this problem step by step. (we should notice the difference of ordered and unordered.)
The simle method is to list all the conditions. Namely select two numbers from the array to judge whether equals to the two numbers. The time complexity is O(n^2). Obviously, we need find a more efficient method.
method1: hash map
If the problem has a serious requrement for the time complexity, we may consider “exchange time with room”. Namely, for the given number, we can whether the other number exists in the array. The time complexity is O(1) and need to query n times, hence the total time complexity is O(n). But the preqeruirement is that we need an O(n) room to construct the hash map.
method2: sorting and move to the middle
If the array is unordered, we should make it ordered firstly. For example, for each a[i], find whether sum - a[i] exists in the original array, and we can use binary search to find sum - arr[i], and it needs long time. Hence, added with the sorting time complexity, the total complexity is O(nlogn + nlogn) = O(nlogn), and the room complexity is O(1).
If the array is ordered, let two pointers begin and end to point to the begin and end of the array. Let begin = 0, end = n -1, and begin++, end—, and judge if a[begin]+a[end] equals to the given sum.
- When a[begin] + a[end] > sum, we need to let the value of a[begin] + a[end] decrease. Hence begin will not change, and end—.
- When a[begin] + a[end]sum, we need to increase the value of a[begin] + a[end]. Hence end will not change, and begin++.
function twoSum(a, sum) {
var begin = 0;
var len = a.length;
var end = len - 1;
while (begin < end) {
var curSum = a[begin] + a[end];
if (curSum === sum) {
console.log(a[begin] + ' ' + a[end]);
break;
} else {
if (curSum < sum) {
begin ++;
} else {
end --;
}
}
}
}
the sum of two fixed value的更多相关文章
- 【POJ1707】【伯努利数】Sum of powers
Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...
- [伯努利数] poj 1707 Sum of powers
题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS Memory Lim ...
- [CLR via C#]16. 数组
数组是允许将多个数据项当作一个集合来处理的机制.CLR支持一维数组.多维数组和交错数据(即由数组构成的数组).所有数组类型都隐式地从System.Array抽象类派生,后者又派生自System.Obj ...
- C++求平均数
题目内容:求若干个证书的平均数. 输入描述:输入数据含有不多于5组的数据,每组数据由一个整数n(n<=50)打头,表示后面跟着n个整数. 输出描述:对于每组数据,输出其平均数,精确到小数点后3位 ...
- 北大ACM(POJ1004-Financial Management)
Question:http://poj.org/problem?id=1004问题点:求平均值及格式化输出. Memory: 248K Time: 0MS Language: C++ Result: ...
- 【南阳OJ分类之语言入门】80题题目+AC代码汇总
小技巧:本文之前由csdn自动生成了一个目录,不必下拉一个一个去找,可通过目录标题直接定位. 本文转载自本人的csdn博客,复制过来的,排版就不弄了,欢迎转载. 声明: 题目部分皆为南阳OJ题目. 代 ...
- 2013腾讯编程马拉松初赛第〇场(3月20日)湫湫系列故事——植树节 HDOJ 4503
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4503 思路:hint from a GOD-COW. 将每一个人模拟成图的一个点,两点连线当且仅当两人是朋 ...
- Financial Management POJ - 1004
Financial Management POJ - 1004 解题思路:水题. #include <iostream> #include <cstdio> #include ...
- noip第6课作业
1. 数据统计 [问题描述] 输入N个整数,求出它们的最小值.最大值和平均值(保留3位小数).输入保证这些数都是不超过1000的整数.(1<=N<=1000) [样例输入] 8 2 ...
随机推荐
- iView栅格的使用
一般情况下栅格系统都会把每行row分为12列,但是iview是采用了24栅格系统,将区域进行24等分 基础用法 实例代码: <template> <Row> <Col s ...
- python-unittest生成报告的几种方式
import unittest suite = unittest.TestSuite() #构造套件 #按测试方法添加 suite.addTest(测试类名('方法名')) suite.addTest ...
- 3D打印格式STL
STL格式及其转换 近期接触了3D打印的一些东西,也制作了一个vrml转stl的插件,对该领域多了一些认识. 目前尚没有打印机直接支持stl.obj等格式,在打印之前需使用厂家提供的软件将stl等格式 ...
- 弹性盒子FlexBox简介(二)
弹性盒子属性 一.align-content属性 属性作用:用于修改flex-wrap属性行为.类似于justify-content,但它不是设置弹性子元素的对齐,而是设置各个行的对齐. 属性值: f ...
- time,sys,os模块
1.time模块 a:结构化时间:struct_time:通过time.localtime获取到一个时间对象,通过这个对象得到对象属性 ****localtime()如果没有参数,默认返回是一个时 ...
- nginx 缓存设置
浏览器缓存原理 浏览器缓存 HTTP协议定义的缓存机制(如:Expires:Cache-control等) 2.浏览器无缓存 3.客户端有缓存 校验过期机制 校验是否过期 ...
- [CSP-S模拟测试]:环(图论+期望)
题目传送门(内部题79) 输入格式 第一行读入两个整数$n,e$表示节点数及$cwystc$已确定的有向边边数. 接下来$e$行,每行两个整数$x,y$描述$cwystc$确定的边. 输出格式 输出一 ...
- 阿里云万网虚拟主机安装配置Https(SSL)教程
太多太多的用户咨询阿里云虚拟主机是否可以安装SSL数字证书?万网空间是否可以支持HTTPS协议访问网站?答案只有一个:目前阿里云虚拟主机都不支持安装SSL证书!但是,但是,可以曲线实现目标! 1.为了 ...
- Codeforces Gym 100269 Dwarf Tower (最短路)
题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...
- Rtmp AAC基本格式(转)
第一个audio data包:AAC sequence header 第二个audio data包:AAC raw AF表示的含义: 1)第一个字节af,a就是10代表的意思是AAC, Format ...