Skip to content

Commit 15a8484

Browse files
authored
Merge pull request neetcode-gh#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

Lines changed: 24 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy