LeetCode 솔루션 분류
[8/22] 342. Power of Four
본문
Easy
2485314Add to ListShareGiven an integer n
, return true
if it is a power of four. Otherwise, return false
.
An integer n
is a power of four, if there exists an integer x
such that n == 4x
.
Example 1:
Input: n = 16 Output: true
Example 2:
Input: n = 5 Output: false
Example 3:
Input: n = 1 Output: true
Constraints:
-231 <= n <= 231 - 1
Follow up: Could you solve it without loops/recursion?
Accepted
387,825
Submissions
857,516
관련자료
-
링크
댓글 2
학부유학생님의 댓글
- 익명
- 작성일
class Solution:
def isPowerOfFour(self, n: int) -> bool:
if n < 1: return False
while not n%4:
n /= 4
return True if n == 1 else False
재민재민님의 댓글
- 익명
- 작성일
Runtime: 2 ms, faster than 60.92% of C++ online submissions for Power of Four.
Memory Usage: 5.9 MB, less than 24.97% of C++ online submissions for Power of Four.
Memory Usage: 5.9 MB, less than 24.97% of C++ online submissions for Power of Four.
class Solution {
public:
bool isPowerOfFour(int n) {
long val = 1;
while(1) {
if(n == val)
return true;
else if(val > n)
break;
else if(val < n)
val *= 4;
}
return false;
}
};