Skip to content

Commit be355da

Browse files
committed
Time: 525 ms (5.14%), Space: 29.8 MB (9.23%) - LeetHub
1 parent 2fbdc63 commit be355da

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

69-sqrtx/69-sqrtx.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution {
2+
public int MySqrt(int x)
3+
{
4+
int l = 0, r = Math.Min(x, int.MaxValue - 1);
5+
while (l < r)
6+
{
7+
var m = l + (r - l + 1) / 2;
8+
try
9+
{
10+
var sq = checked(m * m);
11+
if (sq <= x)
12+
{
13+
l = m;
14+
}
15+
else
16+
{
17+
r = m - 1;
18+
}
19+
}
20+
catch (OverflowException)
21+
{
22+
r = m - 1;
23+
}
24+
}
25+
return l;
26+
}
27+
}

0 commit comments

Comments
 (0)