-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path367_ValidPerfectSquare.py
47 lines (40 loc) · 1.04 KB
/
367_ValidPerfectSquare.py
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
# @Last Modified time: 2016-07-04 11:44:22
class Solution(object):
# Binary Search
def isPerfectSquare(self, num):
low, high = 0, num
while low <= high:
mid = (low + high) / 2
if mid ** 2 == num:
return True
elif mid ** 2 > num:
high = mid - 1
else:
low = mid + 1
return False
class Solution_2(object):
# Truth: A square number is 1+3+5+7+... Time Complexity O(sqrt(N))
def isPerfectSquare(self, num):
i = 1
while num > 0:
num -= i
i += 2
return num == 0
class Solution_3(object):
# Newton Method. Time Complexity is close to constant.
# According to: https://en.wikipedia.org/wiki/Newton%27s_method
def isPerfectSquare(self, num):
val = num
while val ** 2 > num:
val = (val + num / val) / 2
return val * val == num
"""
0
1
121
12321
2147483647
"""