题目:

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. Apache CloudStack多个跨站脚本漏洞(CVE-2013-2136)

    漏洞版本: Apache Group CloudStack 4.1.0 Apache Group CloudStack 4.0.2 Apache Group CloudStack 4.0.1-incu ...

  2. Linux Shell编程(13)——数字常量

    除非一个数字有特别的前缀或符号,否则shell脚本把它当成十进制的数.一个前缀为0的数字是八进制数.一个前缀为0x的数字是十六进制数.一个数用内嵌的#来求值则看成BASE#NUMBER(有范围和符号限 ...

  3. Android学习笔记(三)Application类简介

    每次运行APP时,Application类都保持实例化状态.与Activity不同,配置改变不会导致应用程序重启.通过继承Application类,可以完成一下3项工作: · 对Android运行时广 ...

  4. NGU-学习笔记(1)-动态添加删除图集

    现在 正在做unity的方向 不得不说我选的是UI方向 Unity中很有名的就是NGUI插件了.今天做了个ngui的简单背包系统.非常简陋..初学着 自己mark下 (1)预览 主要就是个 simpl ...

  5. 通过例子学python(2.2)

    2.2 通用序列操作 #2.2 通用序列操作 #所有序列类型都可以进行的操作:索引indexing,分片sliceing,加adding,乘multiplying,成员资格, #计算序列长度,找出最大 ...

  6. Bridges painting - SGU 121(构造)

    题目大意:有个一无向图,给所有的边染色,如果一个点连接的边超过两个,那么最少要染一个白色和一个黑色,能否给整个图染色?不能输出“No solution”. 分析:引用连接 http://edward- ...

  7. js生成随机数的方法实例总结

    js生成随机数主要用到了内置的Math对象的random()方法.用法如:Math.random().它返回的是一个 0 ~ 1 之间的随机数.有了这么一个方法,那生成任意随机数就好理解了.比如实际中 ...

  8. Linux命令之hwclock - 查询和设置硬件时钟

    常用参数 -r, --show         读取并打印硬件时钟(read hardware clock and print result ) -s, --hctosys      将硬件时钟同步到 ...

  9. java websocket工具

    https://github.com/dzharvis/servlet-websocket-server http://redstarofsleep.iteye.com/blog/1488639 ht ...

  10. android反编译经验谈

    反编译这事,找对了工具其实非常简单. 反编译工具下载地址http://pan.baidu.com/s/1eQvshwu android的反编译要经过两个步骤: 1.把dex文件转为jar 2.jar转 ...