[LeetCode] Power of Four 判断4的次方数
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example 1:
Input: 16
Output: true
Example 2:
Input: 5
Output: false
Follow up: Could you solve it without loops/recursion?
Credits:
Special thanks to @yukuairoy for adding this problem and creating all test cases.
这道题让我们判断一个数是否为4的次方数,那么最直接的方法就是不停的除以4,看最终结果是否为1,参见代码如下:
解法一:
class Solution {
public:
bool isPowerOfFour(int num) {
while (num && (num % == )) {
num /= ;
}
return num == ;
}
};
还有一种方法是跟 Power of Three 中的解法三一样,使用换底公式来做,讲解请参见之前那篇博客:
解法二:
class Solution {
public:
bool isPowerOfFour(int num) {
return num > && int(log10(num) / log10()) - log10(num) / log10() == ;
}
};
下面这种方法是网上比较流行的一种解法,思路很巧妙,首先根据 Power of Two 中的解法二,我们知道 num & (num - 1) 可以用来判断一个数是否为2的次方数,更进一步说,就是二进制表示下,只有最高位是1,那么由于是2的次方数,不一定是4的次方数,比如8,所以我们还要其他的限定条件,我们仔细观察可以发现,4的次方数的最高位的1都是计数位,那么我们只需与上一个数 (0x55555555) <==> 1010101010101010101010101010101,如果得到的数还是其本身,则可以肯定其为4的次方数:
解法三:
class Solution {
public:
bool isPowerOfFour(int num) {
return num > && !(num & (num - )) && (num & 0x55555555) == num;
}
};
或者我们在确定其是2的次方数了之后,发现只要是4的次方数,减1之后可以被3整除,所以可以写出代码如下:
解法四:
class Solution {
public:
bool isPowerOfFour(int num) {
return num > && !(num & (num - )) && (num - ) % == ;
}
};
类似题目:
参考资料:
https://leetcode.com/problems/power-of-four/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Power of Four 判断4的次方数的更多相关文章
- [LeetCode] Power of Three 判断3的次方数
Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...
- [LeetCode] Power of Two 判断2的次方数
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
- LeetCode 342. Power of Four (4的次方)
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
- [LeetCode] 326. Power of Three 3的次方数
Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...
- [LeetCode] 342. Power of Four 4的次方数
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- [LeetCode]Power of N
题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是 ...
- 从键盘输入数,输出它们的平方值&判断是不是2的阶次方数
1.从键盘输入两个整数,然后输出它们的平方值和立方值 在Java中,没有像C语言那样有一个专供接受键盘输入值的scanf函数,所以一般的做法是从键盘输入一行字符,保存到字符串s中,再将字符组成的字符串 ...
- Leetcode算法系列(链表)之两数相加
Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...
随机推荐
- 精彩 JavaScript 代码片段
1. 根据给定的条件在原有的数组上,得到所需要的新数组. ——<JavaScript 王者归来> var a = [-1,-1,1,2,-2,-2,-3,-3,3,-3]; functio ...
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- 在Thinkphp中使用AJAX实现无刷新分页
在Thinkphp目录的Lib\ORG\Util\目录里新建AjaxPage.class.php,写入一下内容: <?php // +------------------------------ ...
- 微信小程序
最近在看微信小程序边看边写demo,整体目录结构是一:app.js,app.json,app.wxss这三个文件必须是放在根目录下,二.一个小程序有多个页面组成,其中每个页面的结构是:page.js, ...
- 3.2 js六大数据类型
js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Null,Undefined),和一种混合数据类型(Object). 前面说到js中变量是松散类型的,因此有时候 ...
- java 接口的作用和好处
1.java 接口的作用 http://blog.csdn.net/hack_bug/article/details/7634737 2.一位Java大牛的回答 很多JAVA初级程序员对于接口存在的意 ...
- Allocators与Criterion的相同点及区别
C++98: 1.相同点: Allocators having the same type were assumed to be equal so that memory allocated by o ...
- DLL组件注册器
在实际程序运行中,尤其是绿色软件,都需要对DLL进行注册才能够使用.下面就是笔者开发的一款简单的DLL注册器. http://pan.baidu.com/s/1mhbrN1e
- SQL Server 如何通过SQL语句定位SSRS中的具体报表
在一些IT技术人员的推广.简单培训后,公司很多部门都有一些非IT技术人员参与开发各自需求的Reporting Service报表.原因很简单,罗列出来的原因大概有这样一些: IT部门的考量: 1:IT ...
- 初识Azkaban
先说下hadoop 内置工作流的不足 (1)支持job单一 (2)硬编码 (3)无可视化 (4)无调度机制 (5)无容错机制 在这种情况下Azkaban就出现了 1)Azkaban是什么 Azkaba ...