113.Power of Four
来源:互联网 发布:js date 比较大小 编辑:程序博客网 时间:2024/05/17 22:57
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?
Credits:
Special thanks to @yukuairoy for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
分析:是4的整数幂的整数的二进制长度应该是奇数,并且二进制中只有最高位为1其他均为0.
/** * @date 20160420 * 一个数为4的整幂数,则其对应的二进制一定是奇数位,并且只有最高位是1. */ public boolean isPowerOfFour(int num) { if(num == 0){ return false; } if( Integer.toBinaryString(num).length() % 2 != 1){ return false; }else{ return (num &(num-1)) == 0; } }
0 0
- 113.Power of Four
- [LeetCode]Power of Four
- 342. Power of Four
- Power of Four-Leetcode
- LeetCode Power of Four
- LeetCode - Power of Four
- 342. Power of Four
- LeetCode342. Power of Four
- 342. Power of Four
- Power of Four
- leetcode_342 Power of Four
- LeetCode:Power of Four
- leetcode---Power of Four
- 342. Power of Four
- [LeetCode] Power of Four
- 342. Power of Four
- 342. Power of Four
- 【Leetcode】Power of Four
- 第一次作业(5.安装linux)
- C++第4次实验—双色球问题
- 使用Axure制作App原型应该怎样设置尺寸?
- springmvc结合xheditor实现图文编辑
- tomcat网页发布
- 113.Power of Four
- ubuntu server设置时区和更新时间
- QT中PRO文件写法的详细介绍,很有用,很重要!
- openfire开源服务器的安装与配置
- 消息队列
- Android Fragment嵌套使用存在的一些BUG以及解决方法
- iOS 子视图加入到父视图及移除父视图的过程
- C# double类型转换 0004
- 专利审查指南(2010)