-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvalid-perfect-square.py
More file actions
55 lines (37 loc) · 1.06 KB
/
valid-perfect-square.py
File metadata and controls
55 lines (37 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'''
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt.
Example 1:
Input: 16
Output: true
Example 2:
Input: 14
Output: false
'''
class Solution:
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
# method one 简单的遍历,低效
# for i in range(num+1):
# if i*i > num:
# return False
# if i*i == num:
# return True
# method two 二分查找
# l , r = 0 , num
# while l <= r:
# mid = (l+r)//2
# N = mid ** 2
# if N < num and (mid + 1)**2 > num:
# return False
# if N == num:
# return True
# if N < num:
# l = mid + 1
# if N > num:
# r = mid
# method three 根据数学性质验证,效率比二分法更高
return int(num**0.5) == num**0.5