-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path069. Sqrt(x).cpp
More file actions
31 lines (30 loc) · 849 Bytes
/
069. Sqrt(x).cpp
File metadata and controls
31 lines (30 loc) · 849 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
30
31
// Solution 1. Binary Search
class Solution {
public:
int mySqrt(int x) {
if(x == 0) return x;
int lo = 1, hi = x;
while (true) {
int mid = lo + (hi - lo)/2;
if (mid > x/mid) hi = mid - 1;
else if (mid + 1 > x/(mid + 1)) return mid;
else lo = mid + 1;
}
}
};
// Solution 2. Newton's Method.
/**
* Guess Result Quotient Average Result
* 1 2 / 1 = 2 (2 + 1) / 2 = 1.5
* 1.5 2 / 1.5 = 1.3333 (1.3333 + 1.5) / 2 = 1.4167
* 1.4167 2 / 1.4167 = 1.4118 (1.4167 + 1.4118) / 2 = 1.4142
*/
class Solution {
public:
int mySqrt(int x) {
long r = x;
while (r*r > x)
r = (r + x/r) / 2;
return r;
}
};