题目:

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?

答案:

判断一个数是否是4的幂,不能使用循环和递归:

前面我们已经做过一个数是否为2的幂这道题,4的幂跟2的幂一样,都是最高位为1,其他位为0。

不同的是4的幂中的1在偶数位上,所以我们可以用0xaaaaaaaa与之按位与。

 class Solution {
public:
bool isPowerOfFour(int num) {
return num>&&(!(num&(num-)))&&(!(num&(0xaaaaaaaa)));
}
};

342. Power of Four的更多相关文章

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

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

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

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

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

  4. LeetCode 342. Power of Four

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  5. Leetcode 342 Power of Four 数论

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

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

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

  8. 【一天一道LeetCode】#342. Power of Four

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

随机推荐

  1. 深入浅出Node.js (附录A) - 安装Node

    A.1 Windows系统下的Node安装 A.2 Mac系统下Node的安装 A.3 Linux系统下Node的安装 A.4 总结 A.5 参考资源

  2. 简单的javascript例子

    <html> <head> <title>hongmaju</title> <link rel="shortcut icon" ...

  3. maya绝招(60---尾)

    第64招 置换新意 Displacement(置换)和Bump(凹凸)效果类似,但运行方式不同.将一个File结点用中间拖动到材质上有的shading Group属性中的置换属性上,这个时候可以看到o ...

  4. C++获取本机IP地址

    对网络库简单的封装了一下,以后自己使用的时候方便了很多 #include <WinSock2.h> #pragma comment(lib,"ws2_32") //链接 ...

  5. [置顶] MyEclipse下安装插件方法(properties文件编辑器Propedit为例)

    网上流传了很多安装插件的方法.在这里我只讲解一种方法. 因为我认为这种方法有以下两个优点:第一.简单,方便安装:第二.对于自己安装的插件易于管理. 我的myeclipse版本号为10.5,操作系统为w ...

  6. Maven学习系列二(1-5)

    Maven学习系列二(1-5) 本文转自 QuantSeven 博客,讲解精炼易懂,适合入门,链接及截图如下 http://www.cnblogs.com/quanyongan/category/47 ...

  7. .htaccess文件的妙用

    .htaccess是Apache HTTP Server系统级别的配置文件,通常用来实现主机本身以外的一些功能的,比如说重定向.Gzip.以及访问限制等等………… 1.重定向(301跳转) 相信这个功 ...

  8. DateADD日期Sql

    --1.  当前系统日期.时间 select getdate() --2015-01-06 09:27:27.277   --2.时间操作 dateadd  在向指定日期加上一段时间的基础上,返回新的 ...

  9. memcached与redis 对比

    一. 综述 读一个软件的源码,首先要弄懂软件是用作干什么的,那memcached和redis是干啥的?众所周知,数据一般会放在数据库中,但是查询数据会相对比较慢,特别是用户很多时,频繁的查询,需要耗费 ...

  10. thinkphp中使用PHPEXCEL导入数据

    导入方法比较简单 但必须考虑到Excel本身单元格格式问题 例如以0开头的字符串读出来被去掉了前导0 成为float型而丢失一位 必须进行处理 <?php /** * Author lizhao ...