Skip to content

Commit 1bfc953

Browse files
solves Valid perfect square
1 parent 6fb2b12 commit 1bfc953

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
| 349 | [Intersection of 2 Arrays](https://leetcode.com/problems/intersection-of-two-arrays) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IntersectionOfTwoArrays.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/intersection_of_2_array.py) |
101101
| 350 | [Intersection of 2 Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IntersectionOfTwoArraysII.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/intersection_of_2_arrays_II.py) |
102102
| 359 | 🔒 [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter) | Easy | |
103-
| 367 | [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IsPerfectSquare.java) |
103+
| 367 | [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/IsPerfectSquare.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/valid_perfect_square.py) |
104104
| 374 | [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/GuessNumberHigherOrLower.java) |
105105
| 383 | [Ransom Note](https://leetcode.com/problems/ransom-note) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/RansomNote.java) |
106106
| 387 | [First Unique Character in String](https://leetcode.com/problems/first-unique-character-in-a-string) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/FirstUniqueCharacter.java) |

python/valid_perfect_square.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
# binary search method
3+
def isPerfectSquare(self, number: int) -> bool:
4+
if number == 1:
5+
return True
6+
left, right = 0, number // 2
7+
while left <= right:
8+
middle = (left + right) // 2
9+
mid_square = middle * middle
10+
if mid_square == number:
11+
return True
12+
elif mid_square < number:
13+
left = middle + 1
14+
else:
15+
right = middle - 1
16+
return False
17+
18+
# Newton's Method
19+
# def isPerfectSquare(self, number: int) -> bool:
20+
# r = number
21+
# while r * r > number:
22+
# r = (r + number / r) // 2
23+
# return r * r == number
24+

0 commit comments

Comments
 (0)