Posted on 2023-10-23 14:00
Uriel 阅读(27)
评论(0) 编辑 收藏 引用 所属分类:
闲来无事重切Leet Code 、
大水题
判断一个数是否是4的次方,注意负数和0
写法一
1 #342
2 #Runtime: 16 ms (Beats 49.50%)
3 #Memory: 13.4 MB (Beats 12.41%)
4
5 class Solution(object):
6 def isPowerOfFour(self, n):
7 """
8 :type n: int
9 :rtype: bool
10 """
11 return n > 0 and math.ceil(log10(n) / log10(4)) == int(log10(n) / log10(4))
写法二
1 #342
2 #Runtime: 14 ms (Beats 68.30%)
3 #Memory: 13.1 MB (Beats 88.85%)
4
5 class Solution(object):
6 def isPowerOfFour(self, n):
7 """
8 :type n: int
9 :rtype: bool
10 """
11 if n <= 0:
12 return False
13 while n > 1:
14 if n % 4 != 0:
15 return False
16 n = n // 4
17 return True