您的位置:首页 > 其它

LeetCode 167 Two Sum 2-Input array is sorted

2017-06-10 14:28 357 查看

LeetCode 167 Two Sum 2-Input array is sorted

#include <vector>

using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> result;
int length = numbers.size();
int left = 0;
int right = length - 1;
int tmp = 0;
while (left < right < length && numbers[left] <= target/2)
{
tmp = numbers[left] + numbers[right];
if (tmp < target)
left ++;
if (tmp == target)
{
result.push_back(left + 1);
result.push_back(right + 1);
return result;
}
if (tmp > target)
right --;
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode