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?


题目标签:Bit Manipulation

  这道题目让我们判断一个数字是不是4的次方数,首先排除负数和0。然后利用2的次方数特性,如果 num & (num-1) == 0 的话,那么这个数字是2的次方数 (基于2进制原理)。利用这一点,排除所有不是2的次方数。最后,如果一个数字是4的次方数,那么这个数字 -1 一定可以被3整除。排除掉数字 -1不能被3整除的,剩下的就是4的次方数

Java Solution:

Runtime beats 24.44%

完成日期:06/26/2017

关键词:Bit Manipulation

关键点:基于 num & (num-1) 来判断是不是2的次方数

 public class Solution
{
public boolean isPowerOfFour(int num)
{
if(num <= 0) // if num is 0 or negative number
return false; if((num & (num - 1)) != 0) // if num is not the power of 2
return false; if((num - 1) % 3 != 0) // if num - 1 cannot be divided by 3
return false; return true;
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/5403783.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 342. Power of Four (4的次方)的更多相关文章

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

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

  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(位操作)

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

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

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

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

随机推荐

  1. 前端基础之HTML

    一.HTML 段落是通过 <p> 标签进行定义的 如: <p> hello world! </p> <html> 与 </html> 之间的 ...

  2. 网络配置之基本网络配置(cenos6)

    目录: 关于IP的管理 Linux网卡的卸载与装载 配置网络接口 网络IP配置文件路由管理 路由管理命令 配置动态路由(简介) route的配置文件netstat命令IP命令 ip link 查看网络 ...

  3. 再起航,我的学习笔记之JavaScript设计模式24(备忘录模式)

    备忘录模式 概念介绍 备忘录模式(Memento): 在不破坏对象的封装性的前提下,在对象之外捕获并保存该对象内部的状态以便日后对象使用或者对象恢复到以前的某个状态. 简易分页 在一般情况下我们需要做 ...

  4. MySQL binlog 的恢复操作

     测试出有个问题:mysqlbinlog 不加任何参数 恢复整个binlog 日志文件发现里面有这个操作 SET @@SESSION.GTID_NEXT 的操作,  如果需要恢复文件的时候就需要把他过 ...

  5. U方法

    U方法用于完成对URL地址的组装,特点在于可以自动根据当前的URL模式和设置生成对应的URL地址,格式为:U('地址','参数','伪静态','是否跳转','显示域名');在模板中使用U方法而不是固定 ...

  6. Eight hdu 1043 八数码问题 双搜

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. poj2528 线段树+离散化 (倒序)

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  8. P3377

    题目描述 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或第y个数已经被删除或第x和第y个数在用一个堆 ...

  9. python堆栈实现

    百度百科定义: 堆栈是一个在计算机科学中经常使用的抽象数据类型.堆栈中的物体具有一个特性: 最后一个放入堆栈中的物体总是被最先拿出来, 这个特性通常称为后进先出(LIFO)队列. 堆栈中定义了一些操作 ...

  10. 最好用的css辅助工具——SASS&LESS

    前言 首先,小编给大家解释一下什么是SCSS和LESS,Sass 是一款强化 CSS 的辅助工具,它在 CSS 语法的基础上增加了变量 (variables).嵌套 (nested rules).混合 ...