-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqrt.js
More file actions
43 lines (40 loc) · 1.16 KB
/
sqrt.js
File metadata and controls
43 lines (40 loc) · 1.16 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
/*
* Implement int sqrt(int x).
*
* Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
*
* Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
*
* Example 1:
*
* Input: 4
* Output: 2
* Example 2:
*
* Input: 8
* Output: 2
* Explanation: The square root of 8 is 2.82842..., and since
the decimal part is truncated, 2 is returned.
*/
/**
* @param {number} x
* @return {number}
*/
// say x = 8
var mySqrt = function(x) {
if (x === 0) {return 0;}
let left=1,
right = x-1;
// 1. left=1, right=7 2. left=1, right=4 3. left=2, right=4 4. exit loop since left=2 and right=3
while (left+1 < right){
let mid = parseInt((right + left)/2); // 1. mid is now 4. 2. mid is now 2. 3. mid is now 3
let midSquare = mid * mid; // 1. midSquare = 16 2. midSquare = 4 3. midSquare = 9
if (midSquare <= x){
left = mid; // 2. left = 2
} else {
right = mid; // now left=1 and right=4 3. right is now 3
}
}
// return 2
return left;
};