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. 陕西师范大学第七届程序设计竞赛网络同步赛 I 排队排队排队【数组任一位可以移动到队头,最少移动几次增序/数组指针操作】

    链接:https://www.nowcoder.com/acm/contest/121/I来源:牛客网 题目描述 ACM竞赛队内要开运动会啦!!!! 竞赛队内的一群阳光乐观积极的队员们迅速的在操场上站 ...

  2. 51nod 1091 线段的重叠【贪心/区间覆盖类】

    1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...

  3. hdu 1501 Zipper dfs

    题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...

  4. 获取textview行数

    获取textview行数 textview 代码 import android.content.Context; import android.graphics.Canvas; import andr ...

  5. easyui numberbox precision属性

    //设置easyui numbox 最小值为0,保留2为小数 <input id="payPrice" type="text" name="pa ...

  6. hdu1004(c++)

    字符串统计问题,统计每个字符串的次数,输出出现次数最多的字符串 #include<iostream>#include<string>#include<algorithm& ...

  7. bmp,jpg,png,tif,wmf,emf与eps图片格式转换

    wmf/emf是两种Microsoft Windows的图形文件格式.它是一个矢量图格式,但是也允许包含位图.本质上,一个WMF文件保存一系列可以用来重建图片的Windows GDI命令.在某种程度上 ...

  8. apache 限制某些目录不能访问通过rewrite实现

    通过deny allow访问控制肯定是可以实现的单个目录,但是这个必须指定准确的目录,如果有很多个目录,但是都包含某个名字,比如bbs.1.com/1/tmp/123.htmlbbs.1.com/2/ ...

  9. python pip安装lxml失败(转)

    今天想要试试beautifulsoup4,安装的时候很顺利,然后就准备安装lxml作为解析器,没想到安装时pip直接给我报了一整页的错误. 解决过程 查看了一下错误提示,其中有如下一段: ****** ...

  10. 泳池水面fresnel 的近似替代

    vs float4 ep = TBMultiply(ModelViewMatrix, FinalPosition); DistFromEye.x = TBSaturate( 10.0 + ep.z / ...