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 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
解法一:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> result; for (int i = , j = numbers.size() -; i <= j;) {
if (numbers[i] + numbers[j] == target) {
result.push_back(i + );
result.push_back(j + );
return result;
}
else if (numbers[i] + numbers[j] > target) {
--j;
}
else if (numbers[i] + numbers[j] < target) {
++i;
}
} return result;
}
};
双指针
解法二:
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> result; for (int i = ; i < numbers.size() - ; ++i) {
int start = i + ;
int end = numbers.size() - ;
int temp_target = target - numbers[i]; //binary search
while (start + < end) {
int mid = start + (end - start) / ;
if (numbers[mid] == temp_target) {
result.push_back(i + );
result.push_back(mid + );
return result;
}
else if (numbers[mid] > temp_target) {
end = mid;
}
else if (numbers[mid] < temp_target) {
start = mid;
}
}
if (numbers[start] == temp_target) {
result.push_back(i + );
result.push_back(start + );
return result;
}
if (numbers[end] == temp_target) {
result.push_back(i + );
result.push_back(end + );
return result;
}
} return result;
}
};
二分查找
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 ascending order, find two numbers such that the ...
- 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 - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- 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 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 ...
随机推荐
- @RequestParam注解的使用
自SpringMVC4.2之后,RequestParam内部有4个参数: 1.String name; 2.String value; 3.boolean required; 4.String def ...
- Problem C: 零起点学算法18——3个数比较大小
#include<stdio.h> int main() { int a,b,c; while(scanf("%d %d %d",&a,&b,& ...
- Asp.Net MVC part6 WebAPI
两种web服务SOAP风格:基于方法,产品是WebServiceREST风格:基于资源,产品是WebAPI可以返回json.xml类型的数据对于数据的增.删.改.查,提供相对的资源操作,按照请求的类型 ...
- Linux下CURL设置请求超时时间
使用CURL时,有两个超时时间:一个是连接超时时间,另一个是数据传输的最大允许时间. 连接超时时间用--connect-timeout参数来指定,数据传输的最大允许时间用-m参数来指定. 例如: cu ...
- 【mybatis】mybatis执行一个update方法,返回值为1,但是数据库中数据并未更新,粘贴sql语句直接在数据库执行,等待好久报错:Lock wait timeout exceeded; try restarting transaction
今天使用mybatis和jpa的过程中,发现这样一个问题: mybatis执行一个update方法,返回值为1,但是数据库中数据并未更新,粘贴sql语句直接在数据库执行,等待好久报错:Lock wai ...
- 最佳eclipse字体推荐(个人觉得)
首先大家能够看看这里面推荐的最佳十款字体.http://www.iteye.com/news/11102-10-great-programming-font 可是经过測试发现,排名第一的字体在ecli ...
- VScode格式化vue文件
使用vscode编写vue文件的时候, 发现不能格式化,手动格式化代码,太费劲, 还有可能会出错, 不格式化,有的时候代码编写的时候, 没有很好的管理格式,不美观. 但是我们是可以修改让VScode可 ...
- LeakCanary 的使用遇到的弯路
基本上来源是: http://www.liaohuqiu.net/cn/posts/leak-canary-read-me/ 1. demon 中自带的android_v7兼容包有问题的,建议自己使 ...
- [Spring boot] Application properties and configurations
We can use different application properties application.properties: server.port=9090 application-pro ...
- JavaScript操作dom总结
最近一直忙于新项目,真的挺费心的 从产品原型.ui.接口.真心挺费心的.好多地方都不完善!(i want say F word!!) 基础的东西又需要重新看看了! Node Node.NodeType ...