Skip to content

Commit beaf157

Browse files
committed
367. 有效的完全平方数
1 parent 93415ca commit beaf157

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
|344|[反转字符串](https://leetcode-cn.com/problems/reverse-string/)|[JavaScript](./algorithms/reverse-string.js)|Easy|
8383
|349|[两个数组的交集](https://leetcode-cn.com/problems/intersection-of-two-arrays/)|[JavaScript](./algorithms/intersection-of-two-arrays.js)|Easy|
8484
|350|[两个数组的交集 II](https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/)|[JavaScript](./algorithms/intersection-of-two-arrays-ii.js)|Easy|
85+
|367|[有效的完全平方数](https://leetcode.cn/problems/valid-perfect-square/)|[JavaScript](./algorithms/valid-perfect-square.js)|Easy|
8586
|383|[赎金信](https://leetcode.cn/problems/ransom-note/)|[JavaScript](./algorithms/ransom-note.js)|Easy|
8687
|387|[字符串中的第一个唯一字符](https://leetcode-cn.com/problems/first-unique-character-in-a-string/)|[JavaScript](./algorithms/first-unique-character-in-a-string.js)|Easy|
8788
|389|[找不同](https://leetcode.cn/problems/find-the-difference/)|[JavaScript](./algorithms/find-the-difference.js)|Easy|

algorithms/valid-perfect-square.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number} num
3+
* @return {boolean}
4+
*/
5+
var isPerfectSquare = function (num) {
6+
let left = 1;
7+
let right = num;
8+
9+
// num = 9
10+
// 1 9 mid = 5
11+
// 1 4 mid = 2
12+
// 3 4 mid = 3
13+
14+
while (left <= right) {
15+
let mid = Math.floor((left + right) / 2);
16+
17+
if (mid * mid < num) {
18+
left = mid + 1;
19+
} else if (mid * mid > num) {
20+
right = mid - 1;
21+
} else {
22+
return true;
23+
}
24+
}
25+
26+
return false;
27+
};

0 commit comments

Comments
 (0)