# Square root of LeetCode 69. x

Keywords: Java github calculator network JDK

;

# subject

Implement the int sqrt(int x) function.

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

Because the return type is an integer, the result only retains the integer part, and the decimal part will be rounded off.

Example 1:

Input: 4
Output: 2


Example 2:

Input: 8
Output: 2
Note: the square root of 8 is 2.82842,
Because the return type is an integer, the decimal part will be rounded off.


Source: LeetCode

## Solutions to problems

### Idea 1 - direct sequence calculation

Halve x, then square from 1 to x/2;
Can AC, but low efficiency;
Algorithm complexity:

• Time complexity: ${\ color{Magenta}{\Omicron\left(x\right)}}}$
• Spatial complexity: ${\ color{Magenta}{\Omicron\left(1\right)}}$

### Thinking 2-newton method

Omitted

Algorithm complexity:

• Time complexity: ${\ color{Magenta}{\Omicron\left(logx\right)}}}$
• Spatial complexity: ${\ color{Magenta}{\Omicron\left(1\right)}}$

### Idea 3 - Manual binary search and solution

Start from 1 to x/2 and compare the squares;

There are two ways to write example code: one is iteration, the other is recursion;
Algorithm complexity:

• Time complexity: ${\ color{Magenta}{\Omicron\left(logx\right)}}}$
• Spatial complexity: ${\ color{Magenta}{\Omicron\left(1\right)}}$

### Thinking 4 - pocket calculator, the transformation of the formula of finding roots;

${\color{Magenta}{\sqrt{x}=x^{\frac{1}{2}}=\left(e^{lnx}\right)^{\frac{1}{2}}=e^{\frac{1}{2}lnx}}}$

Directly using the Math class method of JDK;

Algorithm complexity:

• Time complexity: ${\ color{Magenta}{\Omicron\left(1\right)}}$
• Spatial complexity: ${\ color{Magenta}{\Omicron\left(1\right)}}$

## Algorithm source code example

package leetcode;

/**
* @author ZhouJie
* @date 2020 5:11:36 p.m., March 1, 2016
* @Description: 69. x Square root of
*
*/
public class LeetCode_0069 {
}

class Solution_0069 {
/**
* @author: ZhouJie
* @date: 2020 5:33:13 p.m., March 1, 2010
* @param: @param x
* @param: @return
* @return: int
* @Description: 1-Direct sequence calculation verification (speed can be increased by dichotomy);
*
*/
public int mySqrt_1(int x) {
if (x < 2) {
return x;
}
int k = x / 2;
for (int i = 1; i < k + 1; i++) {
if (i * i > x || i * i < 0) {
return i - 1;
} else if (i * i == x) {
return i;
}
}
return k;
}

/**
* @author: ZhouJie
* @date: 2020 5:33:36 p.m., March 1, 2016
* @param: @param x
* @param: @return
* @return: int
* @Description: 2-Newton method;
*
*/
public int mySqrt_2(int x) {
long y = x / 2 + 1;
while (y * y > x) {
y = (y + x / y) / 2;
}
return (int) y;
}

/**
* @author: ZhouJie
* @date: 2020 5:57:44 p.m., March 1, 2010
* @param: @param x
* @param: @return
* @return: int
* @Description: 3-Dichotomy;
*
*/
public int mySqrt_3(int x) {
if (x < 2) {
return x;
}
long left = 1, right = x / 2, mid = 1;
while (left <= right) {
mid = left + (right - left) / 2;
long t = mid * mid;
if (t > x) {
right = mid - 1;
} else if (t < x) {
left = mid + 1;
} else {
return (int) t;
}
}
return (int) right;
}

/**
* @author: ZhouJie
* @date: 2020 5:58:56 p.m., March 1, 2015
* @param: @param x
* @param: @return
* @return: int
* @Description: 4-Displacement recursion is still binary search;
*
*/
public int mySqrt_4(int x) {
if (x < 2) {
return x;
}
int left = mySqrt_4(x >> 2) << 1;
int right = left + 1;
// You must first convert a right to a long rather than a product, which may overflow into a negative number
return (long) right * right > x ? left : right;
}

/**
* @author: ZhouJie
* @date: 2020 1:21:29 p.m., May 9, 2010
* @param: @param x
* @param: @return
* @return: int
* @Description: 5-Pocket calculator, the transformation of the root formula;
*
*/
public int mySqrt_5(int x) {
if (x == 0) {
return x;
}
int r = (int) Math.exp(0.5 * Math.log(x));
return (long) (r + 1) * (r + 1) <= x ? r + 1 : r;
}
}



Posted by turtlefox on Tue, 12 May 2020 02:03:54 -0700