Sqrt(x)
Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.
You must not use any built-in exponent function or operator.
- For example, do not use
pow(x, 0.5)in c++ orx ** 0.5in python.
Example 1:
Input: x = 4 Output: 2 Explanation: The square root of 4 is 2, so we return 2.
Example 2:
Input: x = 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
Constraints:
0 <= x <= 231 - 1
class Solution:
def mySqrt(self, x: int) -> int:
for i in range(1,x+1):
if i*i == x or (i*i<x and (i+1) * (i+1)>x):
return i
return 0
Other:
class Solution:
def mySqrt(self, x: int) -> int:
if x == 0:
return 0
first, last = 1, x
while first <= last:
mid = first + (last - first) // 2
if mid == x // mid:
return mid
elif mid > x // mid:
last = mid - 1
else:
first = mid + 1
return last
My C++:class Solution {
public:
int mySqrt(int x) {
for (long int i=1;i<=x;i++)
if ( i*i == x || i*i < x && (i+1)*(i+1)>x)
{
long int result = i;
return result;
}
return 0;
}
Other C++
Intuition
Approach
We first check if x is 0 or 1. If it is, we know that the square root of 0 and 1 is 0 and 1 respectively, so we directly return x.
For any other value of x, we set up a search range between 1 and x. We initialize two variables start and end to represent the range.
Now comes the clever part: We use a while loop to repeatedly divide the search range in half (Binary Search) to find the square root.
In each iteration of the loop, we calculate the middle value mid using the formula start + (end - start) / 2. This formula ensures that we don't encounter any integer overflow when dealing with large values of x. (This and (start+end)/2) are equivalent mathematically)
Next, we calculate the square of mid and compare it with x.
If the square of mid is greater than x, we know the square root lies in the lower half of the search range. So, we move the end pointer to the left to narrow down the search range.
If the square of mid is equal to x, we have found the square root! So, we return mid as the answer.
If the square of mid is less than x, we know the square root lies in the upper half of the search range. So, we move the start pointer to the right to continue the search.
We repeat steps 4 to 8 until the start pointer becomes greater than the end pointer. At this point, we have found the floor value of the square root, and end holds that value.
To ensure that we return the correct floor value of the square root, we round down the value of end to the nearest integer using the Math.round() method.
Complexity
Time complexity:
The time complexity of this approach is . It's very efficient because Binary Search reduces the search range by half in each iteration, making the search faster.
Space complexity:
The space complexity is , which means the amount of extra memory used is constant, regardless of the input. We only use a few variables to store the search range and the middle value during the computation.
Code
Comments
Post a Comment