双指针

或者哈希,利用两数互补的原则

class Solution {
public:
    vector<int> FindNumbersWithSum(vector<int> array,int sum) {
      std::vector<int> res;
      
      if (array.empty()) {
        return res;
      }
      
      int left = 0, right = array.size() - 1;
      
      while (left < right) {
        if (array[left] + array[right] == sum) {
          res.push_back(array[left]);
          res.push_back(array[right]);
          break;
        } else if (array[left] + array[right] > sum) {
          --right;
        } else {
          ++left;
        }
      }
      
      return res;
    }
};