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. CustomDialog——一个多功能、通用、可自定义的Dialog

    CustomDialog--一个多功能.通用.可自定义的Dialog  依赖 compile 'com.github.SiberiaDante:CustomDialog:v1.0.1'  说明[Cus ...

  2. Hibernate关系映射之many-to-many

    1.建表 2.创建实体类及映射文件 Student.java类 public class Student implements java.io.Serializable { // Fields pri ...

  3. 使用JavaScript实现ATM取款机

    ATM机需求描述如下: 假设一个简单的ATM机的取款过程为:  首先提示用户输入密码(password),假设默认密码为111111,最多只能输入3次,  超过3次则提示用户"密码错误,请取 ...

  4. 如何用kaldi做孤立词识别-初版

    ---------------------------------------------------------------------------------------------------- ...

  5. 51 nod 1521 一维战舰 时间复杂度O(n),同 Codeforces 567D. One-Dimensional Battle Ships 有详细注释

    题目:51nod: 题目Codeforces: 题目注意到两个战舰不能挨在一起就可以了. // 每一段 struct node{ int left; // 段的左端点 int right; // 段的 ...

  6. 【BZOJ】1015 [JSOI2008]星球大战starwar(并查集+离线处理)

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  7. 组合 Lucas定理

    组合 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u [Submit]   [Go Ba ...

  8. NOIP2017SummerTraining0726

    三道比较简单的题,还以为是八校考试的题目,但是并不是,无语了,第三题其实看了挺久的,一看到图,就想到了二分图,网络流之类的算法,但是尽力往这个方向想了好久都没什么思路, 最后从简单入手,然而没什么结果 ...

  9. http://codeforces.com/contest/402/problem/E

    E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...

  10. Centos 7.4 下初探Zabbix安装

    工作一波停一波起,感觉离开.net好久了. 最近工作中发现服务器监视都是用了zabbix,对于我这类不懂的狠狠弥补了一下知识. 无意发现zabbix带有api,就想开发个工具调用api来着.可是api ...