0167.cpp (402B)
1 class Solution { 2 public: 3 vector<int> twoSum(vector<int> &numbers, int target) { 4 int i = 0, j = numbers.size() - 1; 5 while (i < j) { 6 int sum = numbers[i] + numbers[j]; 7 if (sum == target) 8 return {i + 1, j + 1}; 9 else if (sum < target) 10 i++; 11 else 12 j--; 13 } 14 return {}; 15 } 16 };