-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFindSquareRoot.java
45 lines (42 loc) · 1014 Bytes
/
FindSquareRoot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*https://practice.geeksforgeeks.org/problems/square-root/1/*/
//simple binary search approach
class Solution
{
long x;
long floorSqrt(long x)
{
this.x = x;
return findSqrt(1,x);
}
long findSqrt(long low, long high)
{
if (low <= high)
{
long mid = (low+high)/2;
if (mid*mid <= x && (mid+1)*(mid+1) > x) return mid;
if (mid*mid < x) return findSqrt(mid+1,high);
return findSqrt(low,mid-1);
}
return -1;
}
}
/*https://leetcode.com/problems/sqrtx/*/
class Solution {
int x;
public int mySqrt(int x) {
if (x == 0) return 0;
this.x = x;
return findSqrt(1,x);
}
int findSqrt(int low, int high)
{
if (low <= high)
{
int mid = low+(high-low)/2;
if (mid == x/mid) return mid;
if (mid < x/mid) return findSqrt(mid+1,high);
return findSqrt(low,mid-1);
}
return high;
}
}