banner
ekko

ekko's blog

时间不在于你拥有多少,而在于你怎样使用
github
xbox
email

Square root of X

Description#

Implement the int sqrt(int x) function.

Compute and return the square root of x, where x is a non-negative integer.

Since the return type is an integer, the decimal part will be truncated.

Example 1:

Example 2:

Approach#

  • Brute force solution, increment from 1 to find the square, return the value minus one if it exceeds x

Approach 2#

  • Building on the previous approach, increase the increment span for a wider search range, then decrement to find the final value
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.