# Square root of leetcode-069-x

Time：2022-1-5

#### Square root of X

Title Description: implement int sqrt (int x) function.

Calculates and returns the square root of X, where x is a nonnegative integer.

Since the return type is an integer, only the integer part of the result will be retained, and the decimal part will be rounded off.

See leetcode’s official website for an example.

Source: leetcode

###### Solution 1: dichotomy

First, set a minimum value min as 1 and a maximum value Max as 46340`Math.sqrt(Integer.MAX_VALUE)`And then mid is the intermediate value:

• If`mid*mid`If it is equal to x, it returns directlymid
• If`mid*mid`If it is less than x, set min to mid + 1 and make the next round of judgment again;
• If`mid*mid`If it is greater than x, set Max to mid-1 and make the next round of judgment again;
• The termination condition is that Max is not greater than min.

Finally, the mid is returned.

``````public class LeetCode_069 {
public static int mySqrt(int x) {
int min = 1, max = 46340, mid = 1;
mid = (min + max) / 2;
while (min < max) {
int temp = mid * mid;
if (temp == x) {
return mid;
} else if (temp < x) {
min = mid + 1;
} else {
max = mid - 1;
}
mid = (min + max) / 2;
}
return mid;
}

public static void main(String[] args) {
System.out.println(mySqrt(4));
}
}``````

[daily message]Your efforts today are the foreshadowing of luck. Your efforts at present are the flowers of tomorrow.

## (Do not request a bean from a BeanFactory in a destroy method implementation!)

Exception: Singleton bean creation not allowed while singletons of this factory are in destruction (do not request a bean from a beanfactory in a destruction method implementation!) If your project references spring cloud starter Zipkin jar and spring boot starter data redis jar at the same time, redis will not be connected and this exception […]