-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_69.cpp
More file actions
70 lines (65 loc) · 1.6 KB
/
LeetCode_69.cpp
File metadata and controls
70 lines (65 loc) · 1.6 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <vector>
typedef long long ll;
using namespace std;
#define _CRT_SECURE_NO_DEPRECATE
#define __elshorpagi__ (ios_base::sync_with_stdio(false), cin.tie(NULL))
#define edl '\n'
#define sq(x) (x) * (x)
class Solution
{
public:
Solution() { __elshorpagi__; }
int mySqrt_(int x)
{
if (!x || x == 1)
return x;
ll ans(-1), left(1), right(x >> 1);
while (left <= right)
{
ll mid(left + ((right - left) >> 1));
if (sq(mid) == x)
return mid;
else if (sq(mid) < x)
left = mid + 1, ans = mid;
else
right = mid - 1;
}
return ans;
}
int mySqrt(int x)
{
if (!x || x == 1)
return x;
double left(1), right(x >> 1), EPS(1e-9);
while (right - left > EPS)
{
double mid(left + ((right - left) / 2));
if (sq(mid) < x) // it's equal to (sq(mid) - x < 0.0)
left = mid;
else
right = mid;
}
return left + EPS;
// we add EPS cus the result wil be 1.99999999 or something like that;
}
void TEST()
{
int x(4);
cout << mySqrt(x) << edl; // 2
x = 8;
cout << mySqrt(x) << edl; // 2
}
};
int main()
{
Solution sol;
// freopen("../test/input.txt", "r", stdin);
freopen("../test/output.txt", "w", stdout);
int tc(1);
// cin >> tc;
while (tc--)
cout << "Case #" << tc + 1 << edl, sol.TEST();
cout << edl << "DONE" << edl;
return (0);
}