LeetCode 第 342 题(Power of Four)

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?

题目非常easy, 推断一个数是否是 4 的 N 次方。

难点在于后面的附加条件:不能用循环和递归。

首先先给个用递归的解法。

bool isPowerOfFour(int num)
{
if(num == 1) return true;
if(num <= 0) return false;
if(num & 0x03) return false; return isPowerOfFour(num / 4);
}

然后再给一个用循环的解法:

bool isPowerOfFour(int num)
{
if(num < 0) return false;
do
{
if(num == 1) return true;
if(num & 3) return false;
num = num >> 2;
}while (num);
return false;
}

假设不用循环和递归。也是能够做的。比方穷举全部 4 的 N 次方。

尽管这个代码看起来非常丑陋,可是确实也满足题目的要求。

bool isPowerOfFour(int num)
{
switch(num)
{
case 0x01:
case 0x04:
case 0x10:
case 0x40:
case 0x100:
case 0x400:
case 0x1000:
case 0x4000:
case 0x10000:
case 0x40000:
case 0x100000:
case 0x400000:
case 0x1000000:
case 0x4000000:
case 0x10000000:
case 0x40000000:
return true;
default:
return false;
}
}

讲了这么多,该说说正题了。这个题目事实上考察的是这么一个小知识点。 一个数 num。假设是 2 的 N 次方,那么有:

num & (num - 1) = 0

一个数 num 假设是 4 的 N 次方必定也是 2 的 N 次方。所以能够先推断 num 是否是 2 的 N 次方。然后再将 2 的 N 次方中那些不是 4 的 N 次方的数去掉。因此就有了以下的代码。

bool isPowerOfFour(int num)
{
if(num <= 0) return false;
if(num & (num - 1)) return false; // 先推断是否是 2 的 N 次方
if(num & 0x55555555) return true; // 再将不是 4 的 N 次方的数字去掉
return false;
}

LeetCode 第 342 题(Power of Four)的更多相关文章

  1. LeetCode 第 342 题(Power of Four)

    LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether ...

  2. LeetCode 第 231 题 (Power of Two)

    LeetCode 第 231 题 (Power of Two) Given an integer, write a function to determine if it is a power of ...

  3. LeetCode - 326, 342, 231 Power of Three, Four, and Two

    1. 问题 231. Power of Two: 判断一个整数是否是2的n次方,其中n是非负整数 342. Power of Four: 判断一个整数是否是4的n次方,其中n是非负整数 326. Po ...

  4. 【LeetCode每日一题 Day 2】2. 两数相加

    大家好,我是编程熊,今天是LeetCode每日一题的第二天,一起学习的是LeetCode第二题<两数相加>. 题意 给你两个 非空 的链表,表示两个非负的整数.它们每位数字都是按照 逆序 ...

  5. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  6. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

  7. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  8. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  9. LeetCode的刷题利器(伪装到老板都无法diss你没有工作)

    在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...

随机推荐

  1. 传输网页数据的json与xml

    #转载请留言联系 1.json json是数据格式,经常用于在网络中,不同平台或者不同语言中进行数据的传输.json的文件后缀就是 .json.当然,也可以把json直接写在js文件中. json储存 ...

  2. Codeforces Beta Round #91 (Div. 2 Only) A. Lucky Division【暴力/判断是不是幸运数字4,7的倍数】

    A. Lucky Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. Lct浅谈

    Lct浅谈 1.对lct的认识 ​ 首先要知道$lct$是什么.$lct$的全称为$link-cut-tree$.通过全称可以看出,这个数据结构是维护树上的问题,并且是可以支持连边断边操作.$lct$ ...

  4. luogu P1075 质因数分解

    题目描述 已知正整数n是两个不同的质数的乘积,试求出两者中较大的那个质数. 输入输出格式 输入格式: 输入只有一行,包含一个正整数n. 输出格式: 输出只有一行,包含一个正整数p,即较大的那个质数. ...

  5. PHP下载/采集远程图片到本地

    /** * 下载远程图片到本地 * * @param string $url 远程文件地址 * @param string $filenNme 保存后的文件名(为空时则为随机生成的文件名,否则为原文件 ...

  6. zookeeper 学习笔记1(转)

    本文转自https://www.cnblogs.com/fanguangdexiaoyuer/p/7077520.html 感谢作者 可以设置观察的操作:exists,getChildren,getD ...

  7. Facebook KeyHash生成方法

    1. 去https://code.google.com/p/openssl-for-windows/downloads/list下载OpenSSL工具 2.  在C盘根目录下新建一个openssl的文 ...

  8. 【Linux】CentOS7上安装搜狗输入法

    找到一篇资料,提供安装搜狗输入法的方法,在http://www.cnblogs.com/Yiutto/p/6204085.html[也是本文的参考原地址] 请大家自行下载fcitx.tar.gz 1. ...

  9. 粗谈CDN

    CDN:内容分发网络(content delivery network) 1.DNS解析到最快(有可能是地理上最近,也有可能是地理上远但是链路最好)的CDN缓存设备 2.从CDN获取已经缓存的资源 3 ...

  10. web-51job(前程无忧)-账户、简历-数据库设计

    ylbtech-DatabaseDesgin:web-51job(前程无忧)-账户.简历-数据库设计   1.A,数据库关系图 1.B,数据库设计脚本 /App_Data/1,Account.sql ...