题解 | #和为S的两个数字#
和为S的两个数字
https://www.nowcoder.com/practice/390da4f7a00f44bea7c2f3d19491311b
#include <vector> class Solution { public: vector<int> FindNumbersWithSum(vector<int> array,int sum) { if(array.size()<2){ vector<int>ans; return ans; } int i=0,j=array.size()-1; while(i!=j){ if(array[i]+array[j]==sum){ vector<int>ans(2); ans[0]=array[i]; ans[1]=array[j]; return ans; } else if(array[i]+array[j]>sum){ j--; } else{ i++; } } vector<int> ans; return ans; } };