FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/367_Valid_Perfect_Square.java at master · jakehoare/leetcode · GitHub
jakehoare
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
51
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
java
/
367_Valid_Perfect_Square.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
29 lines (24 loc) · 811 Bytes
Breadcrumbs
leetcode
/
java
/
367_Valid_Perfect_Square.java
Copy path
File metadata and controls
29 lines (24 loc) · 811 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
https://leetcode.com/problems/logger-rate-limiter/
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Binary search the range from 1 to n inclusive. Use long for mid to avoid overflow with mid * mid.
Return false if left and right are inverted.
Time - O(log n)
Space - O(1)
*/
public
class
Solution
{
public
boolean
isPerfectSquare
(
int
num
) {
long
left
=
1
;
long
right
=
num
;
while
(
left
<=
right
) {
long
mid
=
left
+ (
right
-
left
) /
2
;
if
(
mid
*
mid
==
num
)
return
true
;
else
if
(
mid
*
mid
>
num
)
// mid too big, sqrt(num) must be LHS of mid
right
=
mid
-
1
;
else
left
=
mid
+
1
;
}
return
false
;
}
}
Back
|
FazBrowse Home
|
New Git URL