-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
46 lines (41 loc) · 889 Bytes
/
main.cpp
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
// https://www.youtube.com/watch?v=JtZBs9Qy_6M&list=PLH8TFsY0qnE2R9kf_9vahNY6pG9601z_4&index=49
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <stack>
#include <vector>
typedef long long LL;
using namespace std;
class Solution {
public:
int mySqrt(int x) {
if (x <= 0) return 0;
int cur = 1;
int res = 1;
int add = 1;
while (cur <= x) {
add += 2;
if (INT_MAX - cur < add) return res;
res += 1;
cur += add;
}
return res - 1;
}
};
#ifdef LZS
int main() {
Solution s;
for (int i = 0; i < 100; i++) {
if (floor(sqrt(i)) != s.mySqrt(i)) {
cout << i << " " << s.mySqrt(i) << endl;
}
}
return 0;
}
#endif