Skip to content

Commit 3183266

Browse files
authored
Merge pull request #56 from Omkar0114/main
Create sqrt.java
2 parents 39b3342 + bd7b643 commit 3183266

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

sqrt.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.company;
2+
//leetcode:https://leetcode.com/problems/sqrtx/
3+
public class Sqrt {
4+
public static void main(String[] args) {
5+
int ans = mySqrt(81);
6+
System.out.println(ans);
7+
}
8+
static int mySqrt(int x) {
9+
if (x == 0) return 0;
10+
int start = 1, end = x;
11+
while (start <= end) {
12+
int mid = start + (end - start) / 2;
13+
if (mid <= x / mid && (mid + 1) > x / (mid + 1))// Found the result
14+
return mid;
15+
else if (mid > x / mid)// Keep checking the left part
16+
end = mid;
17+
else {
18+
start = mid + 1;// Keep checking the right part
19+
}
20+
}
21+
return start;
22+
}
23+
}

0 commit comments

Comments
 (0)