LeetCode 167. Two Sum II - Input array is sorted(双指针)
Posted Shendu.cc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 167. Two Sum II - Input array is sorted(双指针)相关的知识,希望对你有一定的参考价值。
题意:找出数组里两个数字之和为指定数字的两个下标。
题解:双指针
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int left = 0;
int right = numbers.size()-1;
vector<int> ans;
while(left < right)
{
if(numbers[left]+numbers[right]>target)
{
right--;
}
else if(numbers[left]+numbers[right]<target)
{
left++;
}
else
{
ans.push_back(left+1);
ans.push_back(right+1);
break;
}
}
return ans;
}
};
以上是关于LeetCode 167. Two Sum II - Input array is sorted(双指针)的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode167. Two Sum II - Input array is sorted
LeetCode 167. Two Sum II - Input array is sorted
[LeetCode] 167. Two Sum II - Input array is sorted
LeetCode:167. Two Sum II - Input array is sorted