"Leetcode 69" Sqrt (x)

Source: Internet
Author: User

Implement int sqrt(int x) .

Compute and return the square root of X.


Suddenly found that two of the real TM is omnipotent. And Newton's iterative method, math things, headaches don't want to see. and the legendary "magic number" method, more efficient than the math library, tried the next re-.


1 classSolution {2  Public:3     intMYSQRT (intx) {4         5         if(X <0)6             return 0;7         if(x = =0|| x = =1)8             returnx;9         TenUnsignedLong LongBeg =0; OneUnsignedLong LongEnd = (x +1) /2; AUnsignedLong LongMID =0;  -UnsignedLong LongMids =0; -          the          while(Beg <end) -         { -Mid = Beg + (End-beg)/2; -Mids = Mid *mid; +              -             if(Mids = =x) +             { A                 returnmid; at             } -             Else if(Mids >x) -             { -End = Mid-1; -             } -             Else in             { -Beg = mid +1; to             } +         } -          theMids = end *end; *         if(Mids >x) $             returnEnd-1;Panax Notoginseng         Else -             returnend; the     } +};


"Leetcode 69" Sqrt (x)

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.