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 ...
随机推荐
- 【二分】【动态规划】Codeforces Round #393 (Div. 1) B. Travel Card
水dp,加个二分就行,自己看代码. B. Travel Card time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 【博弈论】poj2348 Euclid's Game
假设当前b>a. 一.b%a==0 必胜 二.b<2*a,当前我们没有选择的余地,若下一步是必胜(最终能到情况一),则当前必败:反之,当前必胜. 三.b>2*a,假设x是使得b-ax ...
- Problem I: 打印金字塔
#include<stdio.h> int main() { int n,i,j,k; scanf("%d",&n); ;i<=n;i++) { ;j&l ...
- mormot 直接使用UNIDAC引擎操作数据库
mormot 直接使用UNIDAC引擎操作数据库 MORMOT封装了BDE.FIREDAC.UNIDAC.Nexus 四种通用型数据库引擎,形成了自己独特的数据引擎控件.前提条件是首先要安装通用型数据 ...
- 引用日志log4net.dll的web.config配置
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigu ...
- JavaScript数组api简单说明
1.一个数组加上另一个(一些)数组,不会修改原数组只会返回新数组 arrayObject.concat(arrayX,arrayX,......,arrayX) 2.把数组按照指定字符串分离,不会修改 ...
- nodeJs+socket.io
1.先安装npm和node 2.安装socket.io npm install socket.io 3.html <!DOCTYPE html> <html lang="e ...
- git新建和删除远程分支
创建远程分支: 新建本地分支 git checkout -b branch_name 推送到远程分支,分支名字和本地分支名字相同 git push origin branch_name:branch_ ...
- ISP图像调试工程师——边缘增强(熟悉图像预处理和后处理技术)
http://blog.csdn.net/u013033431/article/details/50907907 http://dsqiu.iteye.com/blog/1638589 概念: 图像增 ...
- Highcharts、AJAX、JSON、JQuery实现动态数据交互显示图表柱形图
上个图给大家看下效果. 点击 查看图表 如下图展示效果 Highcharts简介 Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程 ...