leetcode 167 Two Sum II - Input array is sorted
给定一个有序的数组,和一个整数目标,求两个数的和等于目标的索引,索引从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相关推荐
- 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 ...
- 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 ...
- leetcode python3 简单题167. Two Sum II - Input array is sorted
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...
- [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 ...
- 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...
- 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 ...
- 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 ...
- 167. Two Sum II - Input array is sorted 两数之和 II - 输入有序数组
Title 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: ...
- 167. Two Sum II - Input array is sorted
问题描述 解决方案 class Solution { public:vector<int> twoSum(vector<int>& numbers, int targe ...
最新文章
- java boolean io流_java基础入门-day22-IO流
- PowerBI从SCCM数据库中分析数据和KPI展现
- 特质波动率python
- python无师自通配套资源_Python Tkinter Pack布局管理器(超级详细,看了无师自通)...
- 《Unity虚拟现实开发实战》——第1章,第1.8节小结
- rz安装 xshell_利用XShell上传、下载文件(使用sz与rz命令)
- GDCM:gdcm::IODs的测试程序
- Halcon: 畸变矫正与标定(1)
- GCC常用选项使用详解
- [置顶] 任务三 主题、样式
- Java BigInteger类| 带有示例的减去()方法
- git(10)---Git常用命令
- 测控技术与仪器专业c语言教学视频,测控技术与仪器要学哪些基础和专业课程...
- Ceph rbd cmd练习
- MySql中,复制旧表结构到新表
- C语言之测试程序运行时间
- VC中CListCtrl设置滚动条在最下边的方法(MSDN中的例子)
- 【debian】解决debian中文安装后出现乱码的问题
- 一些python书=待买
- 无法安装NET Framework3.5错误代码0x800F081F