Skip to content

Commit dc6978e

Browse files
committed
Time: 1 ms (85.21%), Space: 41.2 MB (30.08%) - LeetHub
1 parent 3bd3ab8 commit dc6978e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

0069-sqrtx/0069-sqrtx.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)