Skip to content

Commit 9c9a5e0

Browse files
authored
Create 367-valid-perfect-square.js
1 parent af5015d commit 9c9a5e0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

367-valid-perfect-square.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number} num
3+
* @return {boolean}
4+
*/
5+
6+
const isPerfectSquare = function(num) {
7+
let lo = 1
8+
let hi = num
9+
let mid
10+
let val
11+
while(lo <= hi) {
12+
mid = (lo + hi) >>> 1
13+
val = mid * mid
14+
if (val === num) {
15+
return true
16+
}
17+
if (val < num) {
18+
lo = mid + 1
19+
}
20+
if (val > num) {
21+
hi = mid - 1
22+
}
23+
}
24+
return false
25+
};

0 commit comments

Comments
 (0)