1and two numbers for S, using begin,end two position flags and two elements for s before and after, when and greater than s,end forward, and less than s,begin backward
vector<int> Findnumberswithsum (vector<int> Array,intsum) {2vector<int>Res;3 if(array.size () = =0)4 returnRes;5 BOOLFound=false;6 intcursum=0;7 intbegin=0;8 intEnd=array.size ()-1;9 while(begin<end)Ten { Onecursum=array[begin]+Array[end]; A if(cursum==sum) - { - Res.push_back (Array[begin]); the Res.push_back (Array[end]); -Found=true; - Break; - } + Else if(cursum<sum) -begin++; + Else Aend--; at - - } - returnRes; -}
And for the sequential positive integer sequence of s, let small initial 1,big initial for 2,cursum initially for Small+big, when Smal is less than (s+1)/2, big increments, cursum+big; when Cursum is larger than current S, Cur-small, Small forward, if cursum and s are equal, join the result sequence
1 voidAddtoresult (intSmallintbig,vector<int>&num,vector<vector<int>>&Res)2 {3 num.clear ()///The old sequence should be cleared before recording a new sequence4 for(inti=small;i<=big;i++)5 {6 Num.push_back (i);7 }8 res.push_back (num);9 }Tenvector<vector<int> > Findcontinuoussequence (intsum) { Onevector<vector<int>>Res; Avector<int>num; - if(sum==1|| sum==2) - { the returnRes; - } - intSmall=1; - intbig=2; + intMiddle= (1+sum)/2; - intcursum=small+Big; + while(small<middle) A { at if(cursum==sum) - { - Addtoresult (small,big,num,res); - } - while(cursum>sum&&small<middle) - { incursum-=Small; -small++; to if(cursum==sum) + Addtoresult (small,big,num,res); - } thebig++; *cursum+=Big; $ }Panax Notoginseng returnRes; -}
Increments the number of two numbers for s and a sequential positive sequence for s