-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet69.cpp
More file actions
43 lines (43 loc) · 911 Bytes
/
Copy pathleet69.cpp
File metadata and controls
43 lines (43 loc) · 911 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
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
int mySqrt(int x) {
if(x==0)
return 0 ;
int l = 1 ;
int h = x/2+1 ;
// cout << l << ", " << h << endl ;
bool f = false ;
while(l<h){
//cout << l << ", " << h <<endl ;
int m = (l+h)/2 ;
//cout << m << endl ;
if(x/m==m){
f = true ;
return m ;
}
else{
if(x/m>m){
l= m +1;
}else{
h = m-1 ;
}
}
}
//cout << "*** " << l <<endl ;
if(x/l == l)
return l ;
if(!f){
if(x/l>l){
while(x/l>l){
l++ ;
}
return l-1 ;
}else{
while(x/l<l){
l-- ;
}
return l ;
}
}
}
};