Skip to content

Commit c3735c9

Browse files
authored
Create 69. Sqrt(x)
1 parent 73904d1 commit c3735c9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

69. Sqrt(x)

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[https://leetcode.com/problems/sqrtx/description/]
2+
Implement int sqrt(int x).
3+
4+
Compute and return the square root of x.
5+
``` c++
6+
class Solution {
7+
public:
8+
int mySqrt(int x) {
9+
// long long xx=x;
10+
long long left=0;
11+
long long right=x/2+1;
12+
while(left<=right)
13+
{
14+
long long mid=(left+right)/2;
15+
long long sq=mid*mid;
16+
if(sq==x) return mid;
17+
else if(sq<x) left=mid+1;
18+
else right=mid-1;
19+
}
20+
return right;
21+
}
22+
};
23+
```

0 commit comments

Comments
 (0)