LeetCode 342. Power of Four
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?
先说明用循环:
如下
class Solution {
public:
bool isPowerOfFour(int num) {
if(num<)return false;
char position=-;
int i=;
while(i<=)
{
if(num&(<<(i)))
{
if(-!=position&&position!=i)
return false;
position=i;
}
++i;
}
if(position%)return false;
else return true;
}
};
然后我发现题目中提示,你可以不用循环或者递归吗?我就去搜寻了一下这个题目发现http://www.cnblogs.com/grandyang/p/5403783.html
发现第三种方法很巧妙
LeetCode 342. Power of Four的更多相关文章
- [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 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 342 Power of Four 数论
题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. ...
- Python [Leetcode 342]Power of Four
题目描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Examp ...
- [LeetCode] 342. Power of Four(位操作)
传送门 Description Given an integer (signed 32 bits), write a function to check whether it is a power o ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- 231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂
231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...
- leetcode 326. Power of Three(不用循环或递归)
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...
- 342. Power of Four(One-line)
342. Power of Four Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy Given an integer ...
随机推荐
- DIV+CSS
1.可以引入外部的样式:<link rel="stylesheet" type="text/css" href="外部的CSS文件路径&q ...
- java string.format()
String text=String.format("$%1$s 门市价:¥%2$s",18.6,22);$18.6 门市价:¥22
- CPU标志寄存器
这个标志寄存器似乎很重要,不干掉它,中断这玩意还进行不下去了,但是过于复杂,都是一些跟计算结果相关的位,头痛 (这是别人写的一篇非常好的关于标志寄存器的文章http://blog.csdn.net/w ...
- 保留json字符串中文的函数,代替json_encode
// 格式化json中的汉字函数 protected function encode_json($str) { $strs = urldecode(json_encode($thi ...
- C#日志记录函数
错误日志记录在程序运行的实际维护中定位问题具有很大作用,日志越详细,反馈处理问题越方便. 常用的一个B/S架构下的日志函数. //日志记录函数 private void WriteLog( strin ...
- How to implement updatable view with NHibernate
see : http://msdn.microsoft.com/en-us/library/ms187956.aspx(The constrains of creation updatable vie ...
- 转 C# DataTable 和List之间相互转换的方法
一.List/IEnumerable转换到DataTable/DataView 方法一: /// <summary> /// Convert a List{T} to a DataTabl ...
- ANSI C中关于FILE流的一些
ANSI C只是一个定义,定义了一个借口与标准,具体实现将是不同的. 刚看到I/O的时候就对于Stream非常的迷惑,这是什么玩意.后面才明白,这只是一个抽象出来的概念而已.对于一个Stream,它具 ...
- odoo.cli.main()做了什么?
先把代码贴过来 def main(): args = sys.argv[1:] # The only shared option is '--addons-path=' needed to disco ...
- 【Jenkins】jenkins简单搭建并执行任务
part 1 jenkins用户名的配置(gerrit 2.11) #安装jenkins $ sudo apt-get install jenkins #为jenkins用户设定密码 $ sudo ...