Skip to content

Commit 69af5f5

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.6 MB (96.27%)
1 parent 5ef84c5 commit 69af5f5

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p>Given a positive integer num, return <code>true</code> <em>if</em> <code>num</code> <em>is a perfect square or</em> <code>false</code> <em>otherwise</em>.</p>
2+
3+
<p>A <strong>perfect square</strong> is an integer that is the square of an integer. In other words, it is the product of some integer with itself.</p>
4+
5+
<p>You must not use any built-in library function, such as <code>sqrt</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> num = 16
12+
<strong>Output:</strong> true
13+
<strong>Explanation:</strong> We return true because 4 * 4 = 16 and 4 is an integer.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> num = 14
20+
<strong>Output:</strong> false
21+
<strong>Explanation:</strong> We return false because 3.742 * 3.742 = 14 and 3.742 is not an integer.
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= num &lt;= 2<sup>31</sup> - 1</code></li>
29+
</ul>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def isPerfectSquare(self, num: int) -> bool:
3+
# left = bisect_left(range(1, num + 1), num, key=lambda x: x * x) + 1
4+
# return left * left == num
5+
6+
left = 0
7+
right = num
8+
9+
while left <= right:
10+
mid = (left + right) // 2
11+
temp = mid * mid
12+
if num == temp:
13+
return True
14+
elif temp < num:
15+
left = mid + 1
16+
else:
17+
right = mid - 1
18+
19+
return False

0 commit comments

Comments
 (0)