LeetCode-342:Power of Four
2024-10-01 20:30:47
This is another "Pick One" Problem :【Problem:342-Power of Four】
Given an integer (signed bits), write a function to check whether it is a power of . Example:
Given num = , return true. Given num = , return false. Follow up: Could you solve it without loops/recursion?
Python Codes:
class Solution:
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
return num !=0 and num &(num-1)==0 and num & 1431655765==num
答案是如此短小精悍!:
return num != 0 and num &(num-1) == 0 and num & 1431655765== num
1)num != 0:Obviously 0 is not power of 4。
2)num &(num-1) == 0:Any number which is power of 4, it should be power of 2, so use num &(num-1) == 0 to make sure of that.
3)num & 1431655765== num:finally need to check that if the number 'AND' the mask value is itself, to make sure it's in the list above.
最新文章
- Entity Framework Core 1.1 升级通告
- JS原生效果瀑布流布局的实现(一)
- 【vbs】vbs写ini文件
- SET TEXTSIZE number
- fir.im Weekly - 每个程序员都应当拥有的技能树
- CSS中的 backgroundPosition 属性
- Linux_常用命令_03_磁盘/挂载_信息查看
- eclipse 书签
- POJ 1850 Code(组合数)
- Google 常用镜像收集
- 在WIN7/8下把XP装入VHD (下)
- 解决PhpStorm卡顿的问题
- Storm+HBase实时实践
- 在Linux的Terminal中显示文本文件特定行的内容
- Spring 事务与脏读、不可重复读、幻读
- window上安装kafka(单机)
- 软件工程(四)数据流图DFD
- 1066: 单词游戏(game)
- Maven实战(十一)eclipse下载依赖jar包出问题
- SCOJ 4423: Necklace polya