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?

题目很简单, 判断一个数是否是 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. Linux记录用户shell命令

    在/etc/profile中添加下面内容: export LC_ALL=C TMOUT=3600 HISTFILESIZE=2000 HISTSIZE=2000 HISTTIMEFORMAT=&quo ...

  2. python+机器学习 算法用到的知识点总结

    1.浅述python中argsort()函数的用法 (1).先定义一个array数据 1 import numpy as np 2 x=np.array([1,4,3,-1,6,9]) (2).现在我 ...

  3. 【cs229-Lecture3】为什么要选择“最小二乘法”这个指标

    视频地址:http://v.163.com/movie/2008/1/E/B/M6SGF6VB4_M6SGHM4EB.html 具体的推导过程,讲义上都有,已经很详细了.这里的推导过程大都是自己为了练 ...

  4. python基础---->python的使用(六)

    这里记录一下python中关于class类的一些知识.不解释就弄不懂的事,就意味着怎样解释也弄不懂. python中的类知识 一.class的属性引用与实例 class MyClass(): '''A ...

  5. Android SDK更新8.1.0时报错

    Done loading packages.Preparing to install archivesDownloading SDK Platform Android 8.1.0, API 27, r ...

  6. 给用户授予权限时应该尽量避免ANY系统权限

    Oracle推荐给用户授予权限时,给予用户可以完成操作的最小权限.应当尽量避免对用户授予包含ANY的系统权限,如SELECT ANY TABLE,CREATE ANY TABLE等.这些包含ANY的系 ...

  7. Elasticsearch学习之深入搜索五 --- phrase matching搜索技术

    1. 近似匹配 什么是近似匹配,两个句子 java is my favourite programming language, and I also think spark is a very goo ...

  8. CF 445A DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

  9. Mac OS 安装phpMyAdmin

    http://www.coolestguyplanettech.com/installing-phpmyadmin-on-mac-osx-10-7-lion/

  10. iOS的socket开发基础

    目录[-] socket简介 tcp和udp的区别 TCP三次握手和四次挥手 TCP三次握手 tcp四次挥手 tcpsocket和udpsocket的具体实现 tcpsocket的具体实现 udpso ...