Increments the number of two numbers for s and a sequential positive sequence for s

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.