167. Two Sum II
来源:互联网 发布:linux内核的优化 编辑:程序博客网 时间:2024/06/05 00:30
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(2,0);
int len=numbers.size();
int left=0;
int right=len-1;
while(left<=right)
{
if(numbers[left]+numbers[right]==target)
{
result[0]=numbers[left+1];
result[1]=numbers[right+1];
return result;
}
else
{
if(numbers[left]+numbers[right]<target)
left++;
else
right--;
}
}
}
};
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- 167. Two Sum II
- Python笔记Day4
- 线段树(加法和乘法的模板)
- java自定义功能去除字符串两端的空格
- 爬虫基础
- 【数据结构与算法】栈和队列 备忘
- 167. Two Sum II
- fatal error C1853 预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)
- TensorFlow学习笔记3——windows下采用Anaconda时使用tensorboard的方法
- 最新版勤哲Excel服务器V2017.13.0.1无限用户支持手机APP,微信,任意安装,支持后续升级
- unity3d url 解码 编码
- win10 新建、重命名或删除文件夹不自动刷新的问题
- 51nod 1070 Bash游戏 V4(博弈——找规律)
- java中比较字符串的大小(compareTo方法的使用)
- word转HTML 升级版