LeetCode-342. Power of Four
2016-04-20 20:11
239 查看
这道题,主要考察对二进制的理解吧。。
我们知道类似1000,1 0000,10 0000,...是 2 的幂次方,那么怎么样才能是 4 的幂次方呢?就是要求后面的 0 的个数为偶数!
我们知道类似1000,1 0000,10 0000,...是 2 的幂次方,那么怎么样才能是 4 的幂次方呢?就是要求后面的 0 的个数为偶数!
class Solution { public: bool isPowerOfFour(int num) { int num1 = num - 1; if((num&num1)==0){ //insure num is the power of 2 if((num&0x55555555)!=0){ //insure num has even number zero return true; } } return false; } };
相关文章推荐
- 插入排序法
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 拓扑排序
- PHP 函数获取文件名
- 27. Remove Element
- Java NIO写事件处理技巧
- 工资
- Spring框架AOP的配置和实现的简单例子
- Bzoj3473:字符串:广义后缀自动机
- struts2中的Action接收表单传递过来的参数
- PHP算法排序
- 特邀美国EMC实战专家Mark来华授课
- 每日站立会议(三)
- 为mysql 表重新设置自增的主键id
- Wireshark - 观察 ARP 地址解析过程
- 软件需求分析(以美团外卖为例)
- gcc weak/alias/weakref/__typeof()属性总结
- 2016西电校赛网络赛 Problem E 删除字符
- Android基于http的网络请求async-http框架
- 用Python 编写的清除U8系统单据锁定的脚本
- Mysql日志管理