Skip to content

Commit 0476d33

Browse files
authored
Create 0367 Valid Perfect Square.md
1 parent 1ec4f10 commit 0476d33

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

math/0367 Valid Perfect Square.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 367. Valid Perfect Square
2+
https://leetcode-cn.com/problems/valid-perfect-square/
3+
Given a positive integer num, write a function which returns True if num is a perfect square else False.
4+
Follow up: Do not use any built-in library function such as sqrt.
5+
6+
7+
Example 1:
8+
Input: num = 16
9+
Output: true
10+
11+
Example 2:
12+
Input: num = 14
13+
Output: false
14+
15+
Constraints:
16+
1 <= num <= 2^31 - 1
17+
18+
``` python3
19+
class Solution:
20+
def isPerfectSquare(self, num: int) -> bool:
21+
x0=num
22+
while True:
23+
x1=0.5*(x0+num/x0)
24+
if x0-x1<1e-7:
25+
break
26+
x0=x1
27+
return int(x0)*int(x0)==num
28+
```

0 commit comments

Comments
 (0)