# leetcode :Binary Search：Two Sum II - Input array is sorted(069)

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.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

http://www.cnblogs.com/grandyang/p/5185815.html

``````// O(n)
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int l = 0, r = numbers.size() - 1;
while (l < r) {
int sum = numbers[l] + numbers[r];
if (sum == target) return {l + 1, r + 1};
else if (sum < target) ++l;
else --r;
}
return {};
}
};``````

• 本文已收录于以下专栏：

## leetcode 167: Two Sum II - Input array is sorted

Two Sum II - Input array is sorted Total Accepted: 441 Total Submissions: 1017 Given an arra...
• xudli
• 2014年12月31日 08:27
• 8306

## 167. Two Sum II - Input array is sorted [medium] (Python)

• coder_orz
• 2016年08月31日 19:33
• 2265

## LeetCode-Easy部分中标签为 Binary Search 167. Two Sum II - Input array is sorted

• daigualu
• 2017年04月08日 22:20
• 938

## Leetcode-167. Two Sum II - Input array is sorted c语言

1、题目：点击打开链接 2、下面代码是暴力破解，超时无法通过： /** * Return an array of size *returnSize. * Note: The returne...
• tzh476
• 2016年09月01日 20:42
• 574

## LeetCode Two Sum II - Input array is sorted(Two pointer问题)

Given an array of integers that is already sorted in ascending order, find two numbers such that the...
• wuli2496
• 2016年08月19日 13:09
• 281

## 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...
• Leafage_M
• 2018年01月06日 20:45
• 164

## Search in Rotated Sorted Array II -- LeetCode

• linhuanmars
• 2014年03月06日 02:04
• 17882

## LeetCode(108)Convert Sorted Array to Binary Search Tree

• feliciafay
• 2014年01月18日 02:17
• 1917

## Search in Rotated Sorted Array -- LeetCode

• linhuanmars
• 2014年03月05日 09:15
• 19337

## leetcode 81. Search in Rotated Sorted Array II-旋转数组|二分查找

• happyaaaaaaaaaaa
• 2016年06月07日 11:34
• 1451

举报原因： 您举报文章：leetcode :Binary Search：Two Sum II - Input array is sorted(069) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)