一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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的n次方数。

解题思路:可以参考:【一天一道LeetCode】#326. Power of Three【一天一道LeetCode】#231. Power of Two

首先,最简单的方法,采用循环来做:

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

题目中有提问说能不能不用循环和递归来做,和判断3的n次方数一样,4的n次方数在整形数内只有16个,很快就能列举出来,也不失为一个好办法。

另外观察4的n次方数和2的n次方数,可以看出,2的n次方数中能开方得到整数的均为4的n次方数。

class Solution {
public:
    //三个条件:大于0,是2的n次方数,开方后得到整数
    bool isPowerOfFour(int num) {
        return (num>0)&&!(num&(num-1))&&(sqrt(num)*sqrt(num)==num);
    }
};

在leetcode的讨论区看到这样一个答案,很巧妙。

首先判断num是不是2的n次方数,在判断他与0xaaaaaaaa相与是否为0。

4的n次方数有一个特点就是,在bit位上,第0,2,4,6,8,10….位上为1,

所以,先判断是不是3的n次方数,就保证了有且仅有一位上为1的数,然后在剔除奇数位上为1的数,剩下的就是4的n次方数了。

class Solution {
public:
    bool isPowerOfFour(int num) {
        //三个条件:大于0,是2的n次方数,有且仅有偶数位上为1
        return (num>0)&&!(num&(num-1))&&!(num&0xaaaaaaaa);
    }
};

【一天一道LeetCode】#342. Power of Four的更多相关文章

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

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

  4. Leetcode 342 Power of Four 数论

    题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. ...

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

  6. [LeetCode] 342. Power of Four(位操作)

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

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

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

  8. 【一天一道LeetCode】#231. Power of Two

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 【一天一道LeetCode】#326. Power of Three

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. SQL_SERVER_2008升级SQL_SERVER_2008_R2的方法

    SQL 2008升级到SQL 2008 R2. 说到为什么要升级是因为,从另一台机器上备份了一个数据库,到我的机器上还原的时候提示"948错误,意思就是不能把高版本的数据库附加到低版本上,所 ...

  2. C++符号优先级

    C++符号优先级 优先级 操作符 功能 用法 结合性 1 ()[]->.::++-- Grouping operatorArray accessMember access from a poin ...

  3. python2.7练习小例子(二)

        2):题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:2 ...

  4. TeamForge使用指南

    1.什么是TeamForge 可以把TeamForge简单的理解为另外一种github 2.TeamForge的地址 与Project有关,一般会有明确的Link 3.TeamForge登录 用户名和 ...

  5. MySQL创建用户与授权(CentOS6.5)

    1.相关SQL语句 #创建用户与授权方法 ##本地访问 create user 'zend'@'localhost' IDENTIFIED BY '123456'; grant ALL privile ...

  6. map内置函数、lambda表达式、快捷生成想要的列表、filter内置函数

      map函数                             语法 map(function, iterable, ...) 参数 function -- 函数,有两个参数 iterable ...

  7. 剑指架构师系列-MySQL的安装及主从同步

    1.安装数据库 wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-commun ...

  8. 全网代理公开ip爬取(隐藏元素混淆+端口加密)

    简述 本次要爬取的网站是全网代理,貌似还是代理ip类网站中比较有名的几个之一,其官网地址: http://www.goubanjia.com/. 对于这个网站的爬取是属于比较悲剧的,因为很久之前就写好 ...

  9. AutoMagic-开源自动化平台构建思路

    最近在github上看到AutoMagic自动化平台开源了,一时手痒,就试着搭了一套环境,现在把思路和大家说一说. AutoMagic从其工作分工分两部分: 1:Web端管理平台 管理平台基于Pyth ...

  10. Go 语言教程

    Go 语言教程 Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发, ...