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. CentOS7.5下SVN服务器备份与恢复

    可以先查看 svnadmin 命令的使用说明 svnadmin --help 1.完全备份和增量备份 查看 svnadmin dump 命令的使用说明 svnadmin dump --help svn ...

  2. python 打印html源码中xpath

    实例: #coding:utf-8 from lxml import etree import urllib url=urllib.urlopen('http://www.baidu.com').re ...

  3. Echo团队Alpha冲刺随笔 - 第八天

    项目冲刺情况 进展 程序基本完成,根据实际,添加完善新接口 问题 根据功能对接出现的问题继续进行改进 心得 放假了放松下 今日会议内容 黄少勇 今日进展 测试小程序,添加异常和错误操作的处理 存在问题 ...

  4. input 时间字段默认值

    背景: 时间字段展示默认值,开始时间为当天 0点,结束时间为当天晚上12点 代码: <input style="Width: 180px;float:left ;" type ...

  5. 在vue项目中使用自己封装的ajax

    在 src 目录下新建 vue.extend.js ,内容如下: export default { install(Vue) { Vue.prototype.$http=function(option ...

  6. XJCO1711 Procedural Programming

    University of Leeds School of ComputingProcedural Programming XJCO1711Semester 1, 2019-2020Coursewor ...

  7. render函数之jsx应用

    一.模板缺陷(模板的最大特点是扩展难度大,不易扩展.可能会造成逻辑冗余) <level :type="1">哈哈</level> <level :ty ...

  8. python -- 连接 orclae cx_Oracle的使用 二

    转:https://www.cnblogs.com/cyxiaer/p/9396861.html 必需的Oracle链接库的下载地址:https://www.oracle.com/technetwor ...

  9. JavaScript高级程序编程(四)

    2017.7.12  北京 数伏第一天 本日总结: 1.线上服务器时常显示.woff文件丢失解决办法 (IIS服务器) 添加MIME类型 添加三条: 文件扩展名      MIME类型 .svg    ...

  10. Linux上使用Windows软件

    小书匠 安装完成后,甚至可以在linux上进行视频会议,整个过程分两个步骤: 1.安装deepin-wine 这个步骤看github,Jactor Sue这位老哥弄好了,在这: Deepin-Apps ...