问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数。返回这两个数的下标(从1开始),其中第1个下标比第2个下标小。

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

分析:在排序好的数组中进行查找,很容易想到用二分查找的思想。这里相当于是二分查找两个数,可以把最小值和最大值作为起点求和(sum)。

若sum<target,则需要把较小元素也就是low处元素变大,此时不能直接把mid赋值给low,因为假如numbers[mid] + numbers[high] > target,那么可能存在这两个数一个在(low, mid)区域一个在[mid, high]区域的情况,也就是一个小于numbers[mid]的数x满足x + numbers[high] == target成立,此时直接让low向高位进一格即可。

sum>target的情况同样。

解法:

    vector<int> twoSum(vector<int>& numbers, int target) {
int low = ;
int high = numbers.size() - ;
while (low < high) {
int mid = (low + high) >> ;
long sum = numbers[low] + numbers[high];
if (sum == target)
return vector<int>{low + , high + };
else if (sum > target)
high = (numbers[low] + numbers[mid] > target) ? mid : high - ;
else
low = (numbers[mid] + numbers[high] <= target) ? mid : low + ;
}
return vector<int>();
}

比较极端的情况是每次不能移动到二分位置,而是只能进1位或退位,也就是达到O(n)。

但是试了下很难举出这种极端情况的例子,突然直观地觉得好像很难到达O(n)的复杂度……好像还是O(logn)……数学不太好,证明等以后去讨论区看看吧

【Leetcode 167】Two Sum II - Input array is sorted的更多相关文章

  1. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  2. LeetCode算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

  3. leetcode算法:Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  4. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  5. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  6. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  7. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  8. 29. leetcode 167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...

  9. 167. Two Sum II - Input array is sorted@python

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

随机推荐

  1. 【LeetCode 67_字符串_算术运算】Add Binary

    string addBinary(string a, string b) { int alen = a.size(); int blen = b.size(); ) return b; ) retur ...

  2. Talk to customer about the trouble with wireless failure connection。

    It’s upset for  me... 1 ATemel Studio61 and  MKII program failure. Notes: The Flash file : A 90 is w ...

  3. 防火墙---iptables

    一.iptables的说明及环境安装 (1)理论基础:当主机收到一个数据包后,数据包先在内核空间中处理,若发现目的地址是自身,则传到用户空间中交给对应的应用程序处理,若发现目的不是自身,则会将包丢弃或 ...

  4. asp页面快速找到菜单按钮转向的页面的方法

    asp页面快速找到菜单按钮转向的页面的方法: 鼠标放在按钮上,右键属性即可查看

  5. 【Hibernate实战】源码解析Hibernate参数绑定及PreparedStatement防SQL注入原理

        本文采用mysql驱动是5.1.38版本. 本篇文章涉及内容比较多,单就Hibernate来讲就很大,再加上数据库驱动和数据库相关,非一篇文章或一篇专题就能说得完.本文从使用入手在[Spr ...

  6. Android深入理解JNI(二)类型转换、方法签名和JNIEnv

    相关文章 Android深入理解JNI系列 前言 上一篇文章介绍了JNI的基本原理和注册,这一篇接着带领大家来学习JNI的数据类型转换.方法签名和JNIEnv. 1.数据类型的转换 首先给出上一篇文章 ...

  7. Python如何输出带颜色的文字

    print('\033[31m%s' % "这是前景色") 这是前景色 print('%s' % "这是前景色") 这是前景色 print('\033[1;31 ...

  8. slack 团队协作平台

    /**************************************************************************** * slack 团队协作平台 * 说明: * ...

  9. CodeForces - 321E:Ciel and Gondolas (四边形不等式优化DP)

    题意:N个人排成一行,分成K组,要求每组的不和谐值之和最小. 思路:开始以为是斜率优化DP,但是每个区间的值其实已经知道了,即是没有和下标有关的未知数了,所以没必要用斜率. 四边形优化. dp[i][ ...

  10. JS格式化数字(每三位加逗号)

    function toThousands(num) { var num = (num || 0).toString(), result = ''; //判断是否带小数点 if (num.split(' ...