1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <vector> using std::vector; class Solution { public: vector<int> twoSum(vector<int> &numbers, int target) { int left = 0, right = numbers.size() - 1; while (true) { const auto sum = numbers[left] + numbers[right]; if (sum < target) { left++; } else if (sum > target) { right--; } else { return {left + 1, right + 1}; } } } };