您的位置:首页 > 编程语言 > Python开发

Python [Leetcode 342]Power of Four

2016-07-30 20:58 357 查看
题目描述:

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.

解题思路:

位操作。

首先判断是不是只有一位数字为1,其余为0

然后判断为1的位置是不是奇数位

代码如下:

class Solution(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
if num & (num - 1) != 0:
return False
if num & 0x55555555 == 0:
return False

return True


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: