-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrtX.js
More file actions
30 lines (26 loc) · 735 Bytes
/
SqrtX.js
File metadata and controls
30 lines (26 loc) · 735 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
//Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.
//You must not use any built-in exponent function or operator.
//For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
//Runtime Complexity
//Logarithmic, O(log n).
//Memory Complexity
//Linear, O(1).
/**
* @param {number} x
* @return {number}
*/
var mySqrt = function(x) {
let s = 1;
let e = x;
while(s <= e){
const mid = Math.floor((s+e)/2);
const result = mid*mid;
if(result === x || s === mid)
return mid;
else if(result < x)
s = mid;
else
e = mid;
}
return 0;
};