首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。 例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4
输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。 例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4
admin
2019-03-29
50
问题
输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。
例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11。
选项
答案
/////////////////////////////////////////////////////////////////////// // Find two numbers with a sum in a sorted array // Output: ture is found such two numbers, otherwise false /////////////////////////////////////////////////////////////////////// bool FindTwoNumbersWithSum ( int data[], // a sorted array unsigned int length, // the length of the sorted array int sum, // the sum int& num1, // the first number, output int& num2 // the second number, output ) { bool found = false; if(length < 1) return found; int ahead = length - 1; int behind = 0; while(ahead > behind) { long long curSum = data[ahead] + data[behind]; // if the sum of two numbers is equal to the input // we have found them if(curSum == sum) { num1 = data[behind]; num2 = data[ahead]; found = true; break; } // if the sum of two numbers is greater than the input // decrease the greater number else if(curSum > sum) ahead --; // if the sum of two numbers is less than the input // increase the less number else behind ++; } return found; }
解析
如果我们不考虑时间复杂度,最简单想法的莫过去先在数组中固定一个数字,再依次判断数组中剩下的n-1个数字与它的和是不是等于输入的数字。可惜这种思路需要的时间复杂度是O(n2)。
分析:如果我们不考虑时间复杂度,最简单想法的莫过去先在数组中固定一个数字,再依次判断数组中剩下的n-1个数字与它的和是不是等于输入的数字。可惜这种思路需要的时间复杂度是O(n2)。
我们假设现在随便在数组中找到两个数。如果它们的和等于输入的数字,那太好了,我们找到了要找的两个数字;如果小于输入的数字呢?我们希望两个数字的和再大一点。由于数组已经排好序了,我们是不是可以把较小的数字的往后面移动一个数字?因为排在后面的数字要大一些,那么两个数字的和也要大一些,就有可能等于输入的数字了;同样,当两个数字的和大于输入的数字的时候,我们把较大的数字往前移动,因为排在数组前面的数字要小一些,它们的和就有可能等于输入的数字了。
我们把前面的思路整理一下:最初我们找到数组的第一个数字和最后一个数字。当两个数字的和大于输入的数字时,把较大的数字往前移动;当两个数字的和小于数字时,把较小的数字往后移动;当相等时,打完收工。这样扫描的顺序是从数组的两端向数组的中间扫描。
问题是这样的思路是不是正确的呢?这需要严格的数学证明。感兴趣的读者可以自行证明一下。
转载请注明原文地址:https://kaotiyun.com/show/aRmZ777K
0
程序员面试
相关试题推荐
[A]Theperson-skillsmatchapproachtoselection[B]Theimpactsofbadselectiondecisions[C]Theimportanceofstructu
Weakdollarorno,$46,000—thepriceforasingleyearofundergraduateinstructionamidtheredbrickofHarvardYard—is【C1】__
TheMagicofSoundMusicisoneofthemostbeautifulformsofartisticexpressionseverinvented.Inmoviesandplays,musi
重载与覆盖的区别
设置邮件发送于30天之前那么隐藏邮件。
创建一个新的邮件。
在金山毒霸2008中,手动查杀“移动存储设备”中的病毒木马。
在foxpro中定义数据库结构时,字段名的宽度最多可以是()。A.2B.4C.10D.16
在数据库系统中,“事务”是访问数据库并可能更新各种数据项的一个程序执行单元。为了保证数据完整性,要求数据库系统维护事务的原子性、一致性、隔离性和持久性。针对事务的这4种特性,考虑以下的架构设计场景:假设在某一个时刻只有一个活动的事务,为了保证事务
若系统中存在n个等待事务Ti(i=0,1,2,…,n-1),其中:T0正等待被T1锁住的数据项A1,T1正等待被T2锁住的数据项A2,…,Ti正等待被Ti+1锁住的数据项Ai+1,…,Tn-1正等待被T0锁住的数据项A0,则系统处于___________状
随机试题
与慢波睡眠有关的主要神经递质是
图象后处理技术与CT图象重建的本质区别是
图示桁架在竖向外力P作用下的零杆根数为()。
用扣件式钢管脚手架作为支架立柱,且支架立柱为群柱架,其高宽比不应()。
根据《房屋建筑与装饰工程工程量计算规范》(GB50854—2013),措施项目中安全文明施工的“四口”有()。
按管道的设计压力分类,管道分为()
中国古代实行君主专制,古代希腊雅典实行民主政治,古代东西方实行不同的政治制度,主要是由于()。
(2015年真题)下列关于我国司法制度的表述,正确的是()。
在考生文件夹下有一个数据库文件“samp2.accdb”,里面已经设计好了两个表对象“tNorm”和“tStock”。请按以下要求完成设计。创建一个查询,查找库存数量在合理范围([最低储备,最高储备])的产品,并显示“产品名称”和“库存数量”。所建查询
LANDUSEAproblemrelatedtothecompetitionforlanduseiswhethercropsshouldbeusedtoproducefoodorfuel.【1】_____
最新回复
(
0
)