We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 50629df commit 0057b60Copy full SHA for 0057b60
1 file changed
0069-sqrtx/0069-sqrtx.java
@@ -0,0 +1,25 @@
1
+class Solution {
2
+ public int mySqrt(int x) {
3
+ if(x==0 || x==1){
4
+ return x;
5
+ }
6
+
7
+ int left = 1;
8
+ int right = x;
9
10
+ while(left <= right){
11
+ int mid = left + (right-left)/2;
12
13
+ if((mid*mid == x)){
14
+ return mid;
15
+ }else if ((long) mid*mid > (long) x){
16
+ right = mid-1;
17
+ }else{
18
+ left = mid+1;
19
20
21
+ return right;
22
23
24
25
+}
0 commit comments