forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
29 lines (27 loc) · 757 Bytes
/
Solution.cs
File metadata and controls
29 lines (27 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace LeetCodeNet.G0001_0100.S0069_sqrtx {
// #Easy #Top_Interview_Questions #Math #Binary_Search #Binary_Search_I_Day_4
// #Top_Interview_150_Math #2025_07_04_Time_0_ms_(100.00%)_Space_29.30_MB_(44.18%)
public class Solution {
public int MySqrt(int x) {
if (x < 2) {
return x;
}
int left = 1;
int right = x / 2;
int ans = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
if ((long)mid * mid == x) {
return mid;
}
if ((long)mid * mid < x) {
ans = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return ans;
}
}
}