leetcode解题报告(22):Two Sum II - Input array is sorted
描述
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution and you may not use the same element twice.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
分析
用的是二分的思想。
考虑到输入的是递增的有序数组,且有且仅有一组输出结果,因此可以用两个变量,分别从数组起始处i和末尾处j开始,如果两个数相加等于target,就返回这两个下标;如果相加大于target,那么就减小j的值,如果相加小于target,那么就增加i的值,直到找到为止。
代码如下:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
/*
Time exceeded code
int i = 0;
int len = 0;
vector<int>ret;
for(auto it = numbers.begin(); it != numbers.end(); ++it){
int another = target - *it;
auto index = find(it + 1,numbers.end(),another);
if(index != numbers.end()){
len = distance(it,index);
break;
}
++i;
}*/
int i = 0,j = numbers.size() - 1;
while(i < j){
if(numbers[i] + numbers[j] == target){
vector<int>ret = {i + 1,j + 1};
return ret;
}else if(numbers[i] + numbers[j] > target)
--j;
else
++i;
}
}
};
leetcode解题报告(22):Two Sum II - Input array is sorted的更多相关文章
- [LeetCode&Python] Problem 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 the ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 【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 - 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 ...
- LeetCode_167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sorted Easy Given an array of integers that is already sorted in as ...
- leetcode2 Two Sum II – Input array is sorted
Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...
- 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 ...
- 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 ...
- LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告
1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...
随机推荐
- c语言实现串
串 (string)是由零个或者多个字符组成的有限序列,又称字符串 一般表示为 S=“ a1 a2 a3 a4 . . . . . an” 其中S 是串名,双引号串起来的是串值,(有些书用单 ...
- svn钩子(hooks)
目录 钩子脚本的具体写法就是操作系统中shell脚本程序的写法,请根据自己SVN所在的操作系统和shell程序进行相应的写作 所谓钩子就是与一些版本库事件触发的程序,例如新修订版本的创建,或是未版本化 ...
- Scala 面向对象编程之类
定义一个简单的类 // 定义类,包含field以及方法 class HelloWorld { private var name = "leo" def sayHello() { p ...
- vue 下拉刷新数据的插件的使用:
1.安装: npm i vue-scroller -S npm install vue-scroller -D 2.在需要加载的页面中引入,或在公共js文件中引入: import VueScrolle ...
- MySQL 体系结构及存储引擎
MySQL 原理篇 MySQL 索引机制 MySQL 体系结构及存储引擎 MySQL 语句执行过程详解 MySQL 执行计划详解 MySQL InnoDB 缓冲池 MySQL InnoDB 事务 My ...
- hdu1501 记忆化搜索。。。
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- Python3标准库使用样例
原:https://doughellmann.com/blog/the-python-3-standard-library-by-example/the-python-3-standard-libra ...
- javascript/js实现 排序二叉树数据结构 学习随笔
二叉树是一种数据结构.其特点是: 1.由一系列节点组成,具有层级结构.每个节点的特性包含有节点值.关系指针.节点之间存在对应关系. 2.树中存在一个没有父节点的节点,叫做根节点.树的末尾存在一系列没有 ...
- IDEA 中常用快捷键
1.搜索文件(整个项目) ctrl+shift+n 2.最近打开文件 ctrl+e 3.实现接口中方法 ctrl+i 4.跳到上一行 ctrl+alt+enter 5.删除当前行 ctrl+y 6.重 ...
- 爬虫之 BeautifulSoup与Xpath
知识预览 BeautifulSoup xpath BeautifulSoup 一 简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: '' ...