Leetcode Sqrt (x)---java

Source: Internet
Author: User

Implement int sqrt(int x) .

Compute and return the square root of X.

Ask for the square root of a number.

is actually a two-point search.

There are two ways to find

1, is to find the maximum int value, and then from 0 to Max, two points to find.

2, directly for X, from 0 to X, two points to find.

 Public classSolution { Public intMYSQRT (intx) {intmax = 46340; intFlag = MAX/2; if(x >= max*max)returnMax; intStart = 0,end =Max;  while(true){            if(x >= flag*flag) {                if(x < (flag+1) * (flag+1) )                    returnFlag; Else{Start=Flag; }            }Else{                if(x >= (flag-1) * (flag-1))                    returnFlag-1; Else{End=Flag; }} Flag= start+ (End-start)/2; }    }}

Leetcode Sqrt (x)---java

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.