Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit740fc36

Browse files
authored
0367: Valid Perfect Square (#6)
1 parent89e8659 commit740fc36

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

‎0367-Valid_Perfect_Square/main.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// const num = 16;
2+
constnum=14;
3+
4+
functionisPerfectSquare(num:number):boolean{
5+
if(num<2)returntrue;
6+
7+
letleft=2;
8+
letright=num>>1;
9+
while(left<=right){
10+
letmid=left+((right-left)>>1);
11+
letdiv=num/mid;
12+
if(mid===div)returntrue;
13+
if(mid<div){
14+
left=mid+1;
15+
}else{
16+
right=mid-1;
17+
}
18+
}
19+
returnfalse;
20+
}
21+
22+
console.time('isPerfectSquare');
23+
isPerfectSquare(num);
24+
console.timeEnd('isPerfectSquare');
25+
26+
letresult=isPerfectSquare(num);
27+
28+
console.log(result);

‎0367-Valid_Perfect_Square/readme.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#367. Valid Perfect Square
2+
3+
Given a positive integer num, return`true` <i>if`num` is a perfect square or</i>`false` otherwise.
4+
5+
<b>A perfect square</b> is an integer that is the square of an integer. In other words, it is the product of some integer with itself.
6+
7+
You must not use any built-in library function, such as`sqrt`.
8+
9+
##Example 1:
10+
11+
><spanstyle="color:white;">Input: </span>num = 16<br>
12+
><spanstyle="color:white;">Output: </span>true<br>
13+
><spanstyle="color:white;">Explanation: </span>We return true because 4\* 4 = 16 and 4 is an integer.
14+
15+
##Example 2:
16+
17+
><spanstyle="color:white;">Input: </span>num = 14<br>
18+
><spanstyle="color:white;">Output: </span>false<br>
19+
><spanstyle="color:white;">Explanation: </span>We return false because 3.742\* 3.742 = 14 and 3.742 is not an integer.
20+
21+
##Constraints:
22+
23+
- 1 <= num <= 2<sup>31</sup> - 1

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp