class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { int length=numbers.size(); int left,right,cpl=length-1,m; int flag=0; vector<int> ans; for(int i=0;i<=length/2;++i) { left=i+1; right=cpl; while(left<=right) { m=(left+right)/2; if(numbers[i]+numbers[m]==target) { ans.emplace_back(i); ans.emplace_back(m); flag=1; break; } else if(numbers[i]+numbers[m]>target) { right=m-1; } else { left=m+1; } } if(flag) break; } return ans; } };
双指针,左边遍历,右边二分。