标签:back 下标 etc solution sum 双指针 number while problems
题意:找出数组里两个数字之和为指定数字的两个下标。
题解:双指针
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(双指针)
标签:back 下标 etc solution sum 双指针 number while problems
原文地址:https://www.cnblogs.com/dacc123/p/12236057.html