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的位置是不是奇数位
代码如下:
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
相关文章推荐
- LeetCode[342] Power of Four
- LeetCode 342 Power of Four (位运算)
- leetcode 342 Power of four
- LeetCode之342,Power of Four
- 位操作-leetcode 342 Power of Four
- (LeetCode)Power of Four --- 4的次方,python实现
- Power of Four leetcode 342
- leetcode_342 Power of Four
- leetcode——342——Power of Four
- Leetcode 342 Power of Four
- LeetCode 第 342 题(Power of Four)
- [leetcode-342]Power of Four(java)
- LeetCode 342 Power of Four
- Leetcode_342_Power of Four
- leetcode 342 Power of Four
- leetcode:bits:Power of Four(342)
- LeetCode 231,326,342 --Power of Two & Three & Four
- LeetCode 342 Power of Four
- 【LeetCode-342】 Power of Four(C++)
- LeetCode 342 Power of Four