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. 查看 Laravel 的 SQL 语句的方法

    在使用 Laravel 的 Eloquent 进行数据查询的时候,很多小伙伴都想看到背后执行的 SQL 语句到底是什么样的,这小笔录就是解决这个小问题的: 在 Providers/AppService ...

  2. (12)oracle事务

    事物 http://www.cnblogs.com/linjiqin/archive/2012/02/06/2340637.htm 在当前的事务中设置保存点 savepoint  名字; 保存点回滚 ...

  3. #421 Div1 C

    #421 Div1 C 题意 在 (0, n) 和 (m, 0) 处各有一个装置,从起始点(0, 0)出发,首先走短路到 (m, 0) 拿起装置回到起始点,再去 (0, n) 处拿起装置回到起始点.当 ...

  4. JD2

    Business Requirement Support l Develops and communicates plan to manage vendor review of requirement ...

  5. 左偏树自己的一点理解【hdu1512】【Monkey King】

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34693563 向大(hei)佬(e)势力学(di ...

  6. 九. 常用类库、向量与哈希2.Object类

    Object 类位于 java.lang 包中,是所有 Java 类的祖先,Java 中的每个类都由它扩展而来. 定义Java类时如果没有显示的指明父类,那么就默认继承了 Object 类.例如: p ...

  7. zap安装和部署

    1.zap的安装 注意一点测试环境新申请的linux系统java执行路径是/usr/install/java/bin/java,安装zap的时候,会提示找不到java运行环境,解决办法是:做一个软链接 ...

  8. Android.mk入门(一)

    Android.mk是Android工程管理文件,其作用基本等同于Linux环境中的Makefile,在语法上,Android.mk和普通Makefile略有不同,主要区别是Android.mk包含一 ...

  9. Jsp中如何在<c:forEach>标签内获取集合的长度

    利用jstl标签functions的prefix属性的length属性值 1.首先在jsp页面导入jstl function标签 <%@ taglib prefix="fn" ...

  10. 记录一次ceph recovery经历

    一次ceph recovery经历 背景 这是一个測试环境. 该环境中是cephfs 一共12个节点, 2个client.2个mds.8个osd mds: 2颗CPU,每一个4核.一共是8核. 128 ...