题目
Given an integer (signed 32 bits), write a function to check
whether it is a power of 4.
Example:
Given num = 16, return true. Given num = 5, return false.
Follow up: Could you solve it without loops/recursion?
判断一个数是否是4的幂,不用循环和递归
python代码
class Solution(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
return num != 0 and num &(num-1) == 0 and num & 1431655765== num
https://discuss.leetcode.com/topic/42865/python-one-line-solution-with-explanations
本文介绍了一种不使用循环或递归的方法来检查一个32位有符号整数是否为4的幂。通过Python代码实现,利用位运算判断输入数值是否符合4的幂的特征。
746

被折叠的 条评论
为什么被折叠?



