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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. Leetcode 342 Power of Four 数论

    题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. ...

  4. 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 ...

  5. [LeetCode] 342. Power of Four(位操作)

    传送门 Description Given an integer (signed 32 bits), write a function to check whether it is a power o ...

  6. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  7. 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 ...

  8. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  9. 342. Power of Four(One-line)

    342. Power of Four     Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy Given an integer ...

随机推荐

  1. python mysql 更新和插入数据无效

    注意,在删除和增加后必须执行conn.commit()才有效,否则操作无效.

  2. [转]解决ubuntu下面putty不能连接RS232串口(USB2COM线)

    http://m.blog.csdn.net/blog/wuyanrobert_tjsd/33045255 最后还是sudo了,感觉注销后没有加入dialout组

  3. enmo_day_07

    数据备份 物理备份 : 底层数据块 逻辑备份 :exp(export), imp(import) 导入导出工具,提取成dump文件,再将dump文件放入数据库 expdp, impdp 数据蹦 uti ...

  4. Android SQLiteOpenHelper(一)

    SQLiteOpenHelper api解释: A helper class to manage database creation and version management. You creat ...

  5. Ubuntu操作系统安装使用教程 (转)

    随着微软的步步紧逼,包括早先的Windows黑屏计划.实施,逮捕番茄花园作者并判刑,种种迹象表明,中国用户免费使用盗版Windows的日子将不会太长久了,那么这个世界上有没有即免费又易用的操作系统呢? ...

  6. myeclipse中文乱码,JSP页面乱码

    一.设置新建常见文件的默认编码格式,也就是文件保存的格式.在不对MyEclipse进行设置的时候,默认保存文件的编码,一般跟简体中文操作系统(如windows2000,windowsXP)的编码一致, ...

  7. fastcoloredtextbox 中文不重叠

    DrawLineChars方法: private void DrawLineChars(PaintEventArgs e, int firstChar, int lastChar, int iLine ...

  8. SpringMVC 用http请求的Get和Post请求作为路由的方法的重载方式

    @Controller @RequestMapping("/messageProcessing") public class WechatPushController { @Aut ...

  9. Python::OS 模块 -- 简介

    OS 模块简介 OS模块是Python标准库中的一个用于访问操作系统功能的模块,OS模块提供了一种可移植的方法使用操作系统的功能.使用OS模块中提供的接口,可以实现跨平台访问.但是在OS模块中的接口并 ...

  10. CodeBlocks 中fopen函数不支持命令 “r”

    //codeblocks #include<stdio.h> #include<stdlib.h> void main(void) { FILE *fp=NULL; if((f ...