【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 ...
随机推荐
- vue中element 的上传功能
element 的上传功能 最近有个需求,需要在上传文件前,可以进行弹窗控制是否上传upload 看完文档后,感觉有两种思路可以实现 基于before-upload :上传文件之前的钩子,参数为上传的 ...
- js字符串操作方法
1.字符方法: str.charAt(): 可以访问字符串中特定的字符,可以接受0至字符串长度-1的数字作为参数,返回该位置下的字符,如果参数超出该范围,返回空字符串,如果没有参数,返回位置为0的字符 ...
- notepad配合正则表达式处理文本
<option value="irs01.com">irs01.com</option><option value="hdslb.com&q ...
- vue 问题集合 |
vue做类似选项卡 点击改变curIndex , 选项内容显示用 v-show="$inde ...
- jsp页面:js方法里嵌套java代码(是操作数据库的),如果这个js 方法没被调用,当jsp页面被解析的时候,不管这个js方法有没有被调用这段java代码都会被执行?
jsp页面:js方法里嵌套java代码(是操作数据库的),如果这个js 方法没被调用,当jsp页面被解析的时候,不管这个js方法有没有被调用这段java代码都会被执行? 因为在解析时最新解析的就是JA ...
- jsp笔记总结
第一章 JavaWeb简介 1.什么是Web应用程序 什么是Web应用程序是一种通过Web访问的应用程序.Web应用程序一个最大优点就是用户很容易访问.用户只需要有浏览器即可,不需要再安装其他软件.W ...
- [置顶]
滴滴插件化VirtualAPK框架原理解析(二)之Service 管理
在前一篇博客滴滴插件化框架VirtualAPK原理解析(一)之插件Activity管理 中VirtualAPK是如何对Activity进行管理的,本篇博客,我们继续来学习这个框架,这次我们学习的是如何 ...
- boost::function和boost:bind取代虚函数
以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...
- postfix邮件服务器搭建04-终结篇
本来是计划对postfix做一个全系列的安装文档的,不过在查某个知识点的时候,偶然找到一个已经写好的postfix全系列文章,在全部看完之后惊为天人,我认为:总体上会比我要写的要好,所以我准备借用一下 ...
- Maven部署Jetty服务器pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...