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?

Credits:
Special thanks to @yukuairoyfor adding this problem and creating all test cases.

给一个有符号的32位整数,写一个函数检查此数是否为4的次方数。

解法1:位操作

解法2:循环

解法3: 数学函数, 换底公式

Java:

public boolean isPowerOfFour(int num) {
int count0=0;
int count1=0; while(num>0){
if((num&1)==1){
count1++;
}else{
count0++;
} num>>=1;
} return count1==1 && (count0%2==0);
}  

Java:

public boolean isPowerOfFour(int num) {
while(num>0){
if(num==1){
return true;
} if(num%4!=0){
return false;
}else{
num=num/4;
}
} return false;
}

Java:

public boolean isPowerOfFour(int num) {
if(num==0) return false; int pow = (int) (Math.log(num) / Math.log(4));
if(num==Math.pow(4, pow)){
return true;
}else{
return false;
}
}

Python:

class Solution(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
return num > 0 and (num & (num - 1)) == 0 and \
((num & 0b01010101010101010101010101010101) == num)

Python:

# Time:  O(1)
# Space: O(1)
class Solution2(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
while num and not (num & 0b11):
num >>= 2
return (num == 1)

C++:

class Solution {
public:
bool isPowerOfFour(int num) {
while (num && (num % 4 == 0)) {
num /= 4;
}
return num == 1;
}
};

C++:

class Solution {
public:
bool isPowerOfFour(int num) {
return num > 0 && int(log10(num) / log10(4)) - log10(num) / log10(4) == 0;
}
};

C++:

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

C++:  

class Solution {
public:
bool isPowerOfFour(int num) {
return num > 0 && !(num & (num - 1)) && (num - 1) % 3 == 0;
}
};

   

类似题目:

[LeetCode] 231. Power of Two 2的次方数

[LeetCode] 326. Power of Three 3的次方数

All LeetCode Questions List 题目汇总

[LeetCode] 342. Power of Four 4的次方数的更多相关文章

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

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

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

  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. [LeetCode] Power of Four 判断4的次方数

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

随机推荐

  1. python笔记41-虚拟环境virtualenv

    前言 如果你是一个python初学者,我是不建议你搞python虚拟环境的,我看到很多python的初学者同学,使用最新版的pycharm,新建一个工程时候默认就是venu虚拟环境. 然后在使用cmd ...

  2. 快排算法Java版-每次以最左边的值为基准值手写QuickSort

    如题 手写一份快排算法. 注意, 两边双向找值的时候, 先从最右边起找严格小于基准值的值,再从最左边查找严格大于基准base的值; 并且先右后左的顺序不能反!!这个bug改了好久,233~ https ...

  3. 项目Alpha冲刺——总结

    作业描述 课程: 软件工程1916|W(福州大学) 作业要求: 项目Alpha冲刺(团队) 团队名称: 火鸡堂 作业目标: 完成项目Alpha冲刺 团队信息 队名:火鸡堂 队员学号 队员姓名 博客地址 ...

  4. idea拉取最新代码弹窗(Ctrl + T)

    在此设置

  5. python - 对接微信支付(PC)和 注意点

    注:本文仅提供 pc 端微信扫码支付(模式一)的示例代码. 关于对接过程中遇到的问题总结在本文最下方. 参考: 官方文档,    https://blog.csdn.net/lm_is_dc/arti ...

  6. 1、HDFS 架构、启动过程

    Hadoop Distributed File System 易于拓展的分布式文件系统 运行在大量普通廉价机器上,提供容错机制 为大量用户提供性能不错的文件存取服务 NameNode Namenode ...

  7. Redis的通用key操作

    这些操作跟具体的类型没有关系,而是跟key相关. 1.查询Redis中的key的名称: 所有key: 以my开头: 2.删除键: 3.判断某一个键是否存在: 4.重命名: 5.设置过期时间: 如果未设 ...

  8. pgloader 学习(五)pgloader 参考手册

    pgloader将各种来源的数据加载到PostgreSQL中.它可以转换动态读取的数据,并在加载前后提交原始SQL. 它使用COPY PostgreSQL协议将数据流式传输到服务器,并通过填充一对re ...

  9. mybatis的判定时间字段问题 java.lang.IllegalArgumentException: invalid comparison: cn.hutool.core.date.DateTime and java.lang.String

    今天听组员说: mybatis在3.30版本及以上判定时间时 <if test="date_time != null and date_time != '' "> da ...

  10. 洛谷 题解 P1828 【香甜的黄油 Sweet Butter】

    潇洒の开始 第一步:食用头文件和定义变量, 变量干什么用的说的很清楚 #include<iostream> #include<cstdio> #include<cstri ...