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. 厦门海沧区磁盘只有1TB的解决方案

    厦门海沧区磁盘只有1TB的解决方案 1.为WINDOWS 2008 r2 服务器增加5个1T的硬盘 2.打开命令提示符,并键入 diskpart. 3.在“DISKPART”提示符下,键入 list ...

  2. Maximum Size Subarray Sum Equals k -- LeetCode

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  3. POJ 3686 The Windy's (费用流)

    [题目链接] http://poj.org/problem?id=3686 [题目大意] 每个工厂对于每种玩具的加工时间都是不同的, 并且在加工完一种玩具之后才能加工另一种,现在求加工完每种玩具的平均 ...

  4. 在cnBlogs上使用MarsEdit发blog

    工欲善其事,必先利其器.既然决定了要经常使用blog,就要给自己一个好环境! 1.Mac下优秀的发博客工具--MarsEdit 网上有许多有用的文章教你如何使用它. 比如 http://fduo.or ...

  5. UITableView的横向使用

    UITableView只支持竖向显示,要实现横向的显示,需要设置tableView 和cell 的transform属性为CGAffineTransformMakeRotate(-M_PI/2) // ...

  6. 警告Conversion specifies type'int' but the argument has type'size_t'

    代码: #import<Foundation/Foundation.h> int main(int argc,const char * argv[]){ const char *words ...

  7. 编译qt提示找不到gmake

    转:http://wuyuans.com/2012/11/gmake-not-found/ 在用debian编译qt4.5的时候提示gmake: not found,gmake是什么东西,用aptit ...

  8. UVa 816 (BFS求最短路)

    /*816 - Abbott's Revenge ---代码完全参考刘汝佳算法入门经典 ---strchr() 用来查找某字符在字符串中首次出现的位置,其原型为:char * strchr (cons ...

  9. 为什么代理属性设置成assign为了防止生成保留环来

    循环引用 全部的引用计数系统, 都存在循环应用的问题, 比如以下的引用关系: 1. 对象a创建并引用到了对象b 2. 对象b创建并引用到了对象c 3. 对象c创建并引用到了对象b 这时候b和c的引用计 ...

  10. ES6里关于模板字面量的拓展

    JS 的字符串相对其他语言来说功能总是有限的,事实上,ES5中一直缺乏许多特性,如多行字符串.字符串格式化.HTML转义等.ES6通过模板字面量的方式进行了填补,模板字面量试着跳出JS已有的字符串体系 ...