【Leetcode 167】Two Sum II - Input array is sorted
问题描述:给出一个升序排列好的整数数组,找出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的更多相关文章
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- LeetCode算法题-Two Sum II - Input array is sorted
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...
- 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 ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 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 ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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 ...
- 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 ...
随机推荐
- 11g手动打补丁
在两个节点上,分别以oracle用户执行,停止资源 节点一: srvctl stop home -o /app/oracle/11g -s /home/oracle/s1 -n 11grac75 节 ...
- sql复制表结构及复制表数据
一.复制表结构 假设我们有一个数据表Person,有Id,FirstName,LastName,Weight,Height5个列,表结构可以参考这一篇.现在我们想创建一个新表叫People,表结构和P ...
- js 可拉伸表格
table <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- XML方式实现Spring的AOP
1.编写切面类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.fz.an ...
- (转载)Android学习笔记⑨——android.content.ActivityNotFoundException异常处理
异常1:Java.lang.ClassNotFoundException 08-13 18:29:22.924: E/AndroidRuntime(1875):Caused by: Java.lang ...
- 了解jmeter
JMeter是Apache组织的开放源代码项目,100%的用java实现应用.用于压力测试和性能测试.它最初被设计用于Web应用测试但后来扩展到其它测试领域. jmeter和loadrunner的对比 ...
- 2018.11 企业战略课堂笔记2 SWOT-4C战略
1因素列表 2分析过程 :重要的是逻辑推理过程 3 4C战略
- angular使用遇到的问题
http://www.angularjs.cn/A0a6 用路由的时候,在js里写jquery的事件dom操作失效,只有在子页面里嵌套js才生效(jquery也不行).解决办法就是把dom操作用ng- ...
- 安装redis-3.2.10单节点
前段时间安装好的redis,今天用脚本安装的时候突然出现版本异常的问题,所以更新一篇为大家提供参考 本次安装在CentOS6.5,采用的redis-3.2.10,最新的redis-4.0.1安装同样适 ...
- 【解题报告】CF Round #320 (Div. 2)
Raising Bacteria 题意:盒子里面的细菌每天会数量翻倍,你可以在任意一天放任意多的细菌,最后要使得某天盒子里面的细菌数量等于x,求至少要放多少个细菌 思路:显然,翻倍即为二进制左移一位, ...