给定一个有序的数组,和一个整数目标,求两个数的和等于目标的索引,索引从1开始。假设必定存在解。

有两种思路:

直接找:

    vector<int> twoSum(vector<int>& numbers, int target) {int s = 0, e = numbers.size() - 1;while (true) {if (numbers[s] + numbers[e] > target)e--;else if (numbers[s] + numbers[e] < target)s++;else {return vector<int> {s + 1, e + 1};}}}

原理显而易见,最坏情况,也就O(n),当两个索引位于中间位置。

二分查找:

    vector<int> twoSum(vector<int>& numbers, int target) {auto tail = upper_bound(numbers.begin(), numbers.end(), target);auto start = numbers.begin();for (;start != tail;) {int other = target - *start;++start;tail = lower_bound(start, tail, other);if (other == *tail) {vector<int> ret = {start - numbers.begin(), tail - numbers.begin() + 1};return move(ret);}other = target - *tail;--tail;start = lower_bound(start, tail, other);if (other == *start) {vector<int> ret = {start - numbers.begin() + 1, tail - numbers.begin() + 2};return move(ret);}}}

每次都是二分地前进后退,最坏情况下达到O(nlogn),当两索引位于中间。

但当两个答案靠近某一端时性能较好。就leetcode结果来看,相差不大。

转载于:https://www.cnblogs.com/willaty/p/8376786.html

leetcode 167 Two Sum II - Input array is sorted相关推荐

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

    题目 : Given an array of integers that is already sorted in ascending order, find two numbers such tha ...

  2. LeetCode 167. Two Sum II - Input array is sorted--Python解法

    题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...

  3. leetcode python3 简单题167. Two Sum II - Input array is sorted

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...

  4. [LeetCode By Python]167. Two Sum II - Input array is sorted

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

  5. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

  6. 167. Two Sum II - Input array is sorted (C, C++, Python)

    本文讲述了Array类中第167个问题的几种解法,实现语言包括C,Python以及C++. 问题: Given an array of integers that is already sorted ...

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

    1.题目 Given an array of integers that is already sorted in ascending order, find two numbers such tha ...

  8. 167. Two Sum II - Input array is sorted 两数之和 II - 输入有序数组

    Title 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: ...

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

    问题描述 解决方案 class Solution { public:vector<int> twoSum(vector<int>& numbers, int targe ...

最新文章

  1. java boolean io流_java基础入门-day22-IO流
  2. PowerBI从SCCM数据库中分析数据和KPI展现
  3. 特质波动率python
  4. python无师自通配套资源_Python Tkinter Pack布局管理器(超级详细,看了无师自通)...
  5. 《Unity虚拟现实开发实战》——第1章,第1.8节小结
  6. rz安装 xshell_利用XShell上传、下载文件(使用sz与rz命令)
  7. GDCM:gdcm::IODs的测试程序
  8. Halcon: 畸变矫正与标定(1)
  9. GCC常用选项使用详解
  10. [置顶] 任务三 主题、样式
  11. Java BigInteger类| 带有示例的减去()方法
  12. git(10)---Git常用命令
  13. 测控技术与仪器专业c语言教学视频,测控技术与仪器要学哪些基础和专业课程...
  14. Ceph rbd cmd练习
  15. MySql中,复制旧表结构到新表
  16. C语言之测试程序运行时间
  17. VC中CListCtrl设置滚动条在最下边的方法(MSDN中的例子)
  18. 【debian】解决debian中文安装后出现乱码的问题
  19. 一些python书=待买
  20. 无法安装NET Framework3.5错误代码0x800F081F

热门文章

  1. PAT、PMT、SDT详解
  2. Listary-不仅仅是快速检索文件
  3. CCNP-EIGRP不等价负载均衡
  4. centos 7 mysql图形界面_centos7-vnstat图形界面搭建
  5. 如何限制用户的内存使用量
  6. Nginx中添加gzip_static支持
  7. zookeeper的名词复盘-版本-保证分布式数据原子性
  8. 高仿真的类-ApplicationContext
  9. MapReduce运行机制-Map阶段
  10. IDEA 项目结构旁边出现 0%classes,0% lines covered