【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 ...
随机推荐
- ubuntu12.04+openni+nit+SensorKinect环境搭建
一.安装openni 1.下载openni OpenNI-Bin-Dev-Linux-x64-v1.5.4.0.tar.bz2 2.cd ~; mkdir kinec ...
- 解决xadmin下设置“use_bootswatch = True”无效的问题
环境:python 2.7django 1.9xadmin采用源代码的方式引入到项目中QQ群交流:697028234 1.安装requests pip install requests 2./xadm ...
- iOS跳转支付宝付款码和扫一扫页面
iOS跳转支付宝付款码和扫一扫页面 // 是否支持支付宝 NSURL * myURL_APP_A = [NSURL URLWithString:@"alipay://"]; if ...
- SQL2008R2 收缩数据库问题 - 日志文件不变小
数据库的日志文件(*.ldf)越来越大,怎么办? 收缩吧.收缩日志文件的操作真不简单哟,还跟数据库的恢复模式有关啊. 一.“简单恢复模式”时的日志收缩 1. 截断日志 当数据库的恢复模式为“简单”的时 ...
- 第8课:异常处理、面向对象编程、发送邮件、url编码
1. 异常处理 import traceback import pymysql import requests def calc(a, b): res = a / b return res def m ...
- New Concept English Two 33 94
$课文92 自找麻烦 1016. It must have been about two in the morning when I returned home. 我回到家时,肯定已是凌晨两点左右了 ...
- vue.js 源代码学习笔记 ----- instance event
/* @flow */ import { updateListeners } from '../vdom/helpers/index' import { toArray, tip, hyphenate ...
- echarts-detail--柱状图
一:柱形图 1.Echarts-柱状图柱图宽度设置-----只需要设置series中的坐标系属性barWidth就可以 /** * 堆积柱状图 * @param xaxisdata x轴:标签(数组) ...
- 评价指标的计算:accuracy、precision、recall、F1-score等
记正样本为P,负样本为N,下表比较完整地总结了准确率accuracy.精度precision.召回率recall.F1-score等评价指标的计算方式: (右键点击在新页面打开,可查看清晰图像) 简单 ...
- 7.翻转句子中单词的顺序[ReverseWordOrderInSentence]
[题目] 输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理. 例如输入“I am a student.”,则输出“stud ...