题意:找出数组里两个数字之和为指定数字的两个下标。
题解:双指针
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;
}
};