[LeetCode]342. Power of Four (Easy) Python-2023.10.23
Posted on 2023-10-23 14:00 Uriel 閱讀(37) 評(píng)論(0) 編輯 收藏 引用 所屬分類: 閑來無事重切Leet Code 、大水題判斷一個(gè)數(shù)是否是4的次方,注意負(fù)數(shù)和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))
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
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