Skip to content

Commit 15a8484

Browse files
authored
Merge pull request #2644 from aadil42/patch-68
2 parents e35eeaa + 07dfbff commit 15a8484

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

javascript/0069-sqrtx.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Binary Search
3+
* https://leetcode.com/problems/sqrtx/
4+
*
5+
* Time O(log(n)) | Space O(1)
6+
* @param {number} x
7+
* @return {number}
8+
*/
9+
var mySqrt = function(x) {
10+
let left = 1;
11+
let right = x;
12+
13+
while(left <= right) {
14+
const mid = (left + right) >> 1;
15+
if(mid * mid <= x && (mid+1) * (mid+1) > x) return mid;
16+
if(mid * mid < x) {
17+
left = mid + 1;
18+
} else {
19+
right = mid -1;
20+
}
21+
}
22+
23+
return 0;
24+
};

0 commit comments

Comments
 (0)