一天一道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. ubuntu Linux下C语言open函数打开或创建文件与read,write函数详细讲解

    open(打开文件) 相关函数 read,write,fcntl,close,link,stat,umask,unlink,fopen 表头文件 #include<sys/types.h> ...

  2. Redis wind7 安装

    下载地址:https://github.com/MSOpenTech/redis/releases. Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择,这里我们下载 Red ...

  3. Windows10下配置python的环境变量

    从官网下载Windows下的python版本,一路按照默认进行安装. 安装之后配置环境变量的步骤如下: 1,点"我的电脑",右键选"属性". 2,选择" ...

  4. python中没有字符(char)这一基本数据类型

    感觉受C语言的影响太大了,一开始以为python中也会有字符这一基本数据类型,后来遇到了很多问题,这才发现python中压根没有这一数据类型( ╯□╰ ). 吐槽一下:感觉python还真是'够简单啊 ...

  5. POJ-2299 Ultra-QuickSort---树状数组求逆序对+离散化

    题目链接: https://vjudge.net/problem/POJ-2299 题目大意: 本题要求对于给定的无序数组,求出经过最少多少次相邻元素的交换之后,可以使数组从小到大有序. 两个数(a, ...

  6. text-align:center属性失效

    text-align:center只对inline元素有效,失效的情况下 给它所有的子元素加上 display:inline-block即可 inline-block不兼容ie6

  7. ORACLE设置自启动记录

    设置开机自启动1. 修改Oracle系统配置文件:/etc/oratab,只有这样,Oracle 自带的dbstart和dbshut才能够发挥作用.[root@hailiang ~]# vi /etc ...

  8. Java Spring boot 2.0 跨域问题

    跨域 一个资源会发起一个跨域HTTP请求(Cross-site HTTP request), 当它请求的一个资源是从一个与它本身提供的第一个资源的不同的域名时 . 比如说,域名A(http://dom ...

  9. python while条件和if判断的总练习

    输出123456 89的数字 num =1 while num < 11: if num == 7: pass else: print(num) num = num + 1 输出1-100的奇数 ...

  10. Stall Reservations

    Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked ...