在排序数组中查找和为定值的两个数

题目:输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字,要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。

例如输入数组1, 2, 4,7, 11, 15和数字15。由于4+11=15,因此输出是4和11。

分析:如果不考虑时间复杂度,最简单想法莫过于先在数组中固定一个数字,再依次判断数组中剩下的n-1个数字与它的和是不是等于输入的数字,但是这种思路的时间复杂度是O(n2)。

假设现在随便在数组找到两个数,如果它们的和等于输入的数字,那就找到了;如果小于输入数字,则把两个数的和调整大一点,把较小的数字往后面移动一个数字;如果大于输入数字,则把两个数的和调整小一点,把较大的数字往前面移动一个数字;这样它们的和就有可能等于输入的数字了。

总结起来,最初找到数组的第一个数字和最后一个数字,当两个数字的和大于输入的数字时,把较大的数字往前移动;当两个数字的和小于输入的数字时,把较小的数字往后移动;当然也有可能出现没有等于输入数字的情况,要加以判断。

代码如下:

/////////////////////////////////////////////////////////////////
// Find two numbers with a sum in a sorted array
// Output: true is found such two numbers, otherwise false
/////////////////////////////////////////////////////////////////
bool FindTwoNumbersWithSum
(
int data[], // a sorted array
unsigned int length, // the length of thesorted 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, then 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, delete 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;
}

【Data Structure & Algorithm】在排序数组中查找和为定值的两个数的更多相关文章

  1. (1)Two Sum--求数组中相加为指定值的两个数

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  2. Leetcode算法【34在排序数组中查找元素】

    在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...

  3. [简单-剑指 Offer 53 - I. 在排序数组中查找数字 I]

    [简单-剑指 Offer 53 - I. 在排序数组中查找数字 I] 统计一个数字在排序数组中出现的次数. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出 ...

  4. Java实现 LeetCode 34 在排序数组中查找元素的第一个和最后一个位置

    在排序数组中查找元素的第一个和最后一个位置 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n ...

  5. 每日一题 - 剑指 Offer 53 - I. 在排序数组中查找数字 I

    题目信息 时间: 2019-07-04 题目链接:Leetcode tag:二分查找 哈希表 难易程度:简单 题目描述: 统计一个数字在排序数组中出现的次数. 示例1: 输入: nums = [5,7 ...

  6. [LeetCode]面试题53 - I. 在排序数组中查找数字 I(二分);面试题53 - II. 0~n-1中缺失的数字(二分)

    ##面试题53 - I. 在排序数组中查找数字 I ###题目 统计一个数字在排序数组中出现的次数. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: 2 ...

  7. 剑指 Offer 53 - I. 在排序数组中查找数字 I + 二分法

    剑指 Offer 53 - I. 在排序数组中查找数字 I Offer_53_1 题目描述 方法一:使用HashMap package com.walegarrett.offer; /** * @Au ...

  8. 【LeetCode】34. 在排序数组中查找元素的第一个和最后一个位置

    34. 在排序数组中查找元素的第一个和最后一个位置 知识点:数组,二分查找: 题目描述 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置 ...

  9. 力扣 - 剑指 Offer 53 - I. 在排序数组中查找数字 I

    题目 剑指 Offer 53 - I. 在排序数组中查找数字 I 思路1 一般来说,首先想到的是使用一个变量,从头开始遍历整个数组,记录target数组出现的次数,但是这样的时间复杂度是O(n),还是 ...

随机推荐

  1. swich-----case语句的用法

    转:  http://xinzhi.wenda.so.com/a/1517927252619839

  2. C语言--函数篇

      1-1.函数简单调用 1 #include<stdio.h> 2 #include<string.h> 3 #include<windows.h> 4 int ...

  3. GoogleFusionTablesAPI初探地图与云计算

    http://developer.51cto.com/art/200906/129324.htm http://yexiaochai.iteye.com/blog/1893735 http://yex ...

  4. h5的复制功能

    js+html5实现复制文字按钮 <div> <input type="text" name="guanfangaddress" id=&qu ...

  5. PythonCookBook笔记——数字日期和时间

    数字日期和时间 数字的四舍五入 用round函数,指定值和小数位数. >>> round(1.23, 1) 1.2 >>> round(1.27, 1) 1.3 & ...

  6. C语言,简单计算器【上】

    由于工作需要最近在研究PHP扩展,无可避免的涉及到了C语言.从出了学校以后C语言在实际工作中还没有用到过,所以必须要先进行一点复习工作.个人认为对于熟悉一样东西说最好的方法是上手实践.于是便想起了当时 ...

  7. 【BZOJ4956】lydsy七月月赛 I 乱搞

    [BZOJ4956]lydsy七月月赛 I 题面 题解:傻题,Floyd传递闭包即可~ #include <cstdio> #include <cstring> #includ ...

  8. ZOJ 3551 Bloodsucker <概率DP>

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3551 题意:开始有N-1个人和一个吸血鬼, 每天有两个生物见面,当人 ...

  9. 5 Ways to Send Email From Linux Command Line

    https://tecadmin.net/ways-to-send-email-from-linux-command-line/ We all know the importance of email ...

  10. codeforces B. Bear and Strings 解题报告

    题目链接:http://codeforces.com/problemset/problem/385/B 题目意思:给定一条只有小写英文组成的序列,需要找出至少包含一个“bear”的单词的子序列个数.注 ...