原题链接在这里:https://leetcode.com/problems/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?

题解:

Power of TwoPower of Three类似。

每次iteration若是不能被4整除即return false. 若可以被4整除,便除以4, 直到结果等于1.

Time Complexity: O(log(num)). Space: O(1).

AC Java:

 public class Solution {
public boolean isPowerOfFour(int num) {
if(num<=0){
return false;
}
while(num%4 == 0){
num /= 4;
}
return num==1;
}
}

Follow up 需要no loops/recursion.

可以bit manipulation, 首先判定num是否为2的幂数. 若是2的幂数, num二进制表达首位为1, num-1除首位均为1. num & num-1 应等于 0.

然后判定首位的1是在奇数位置上, eg. 10000 = 16. 所以 num & 0x55555555 应等于num 原值.

Time Complexity: O(1). Space: O(1).

 public class Solution {
public boolean isPowerOfFour(int num) {
return num>0 && (num & num-1) == 0 && (num & 0x55555555) == num;
}
}

LeetCode Power of Four的更多相关文章

  1. [LeetCode] Power of Four 判断4的次方数

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...

  2. [LeetCode] Power of Three 判断3的次方数

    Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...

  3. [LeetCode] Power of Two 判断2的次方数

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  4. LeetCode Power of Three

    原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...

  5. Leetcode Power of Two

    Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...

  6. Leetcode Power of two, three, four

    Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...

  7. leetcode power(x,n)

    class Solution { public: double pow(double x, int n) { double a=1; if(n==0)return 1; if(x==1)return ...

  8. LeetCode——Power of Two

    Description: Given an integer, write a function to determine if it is a power of two. public class S ...

  9. [LeetCode]Power of N

    题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是 ...

随机推荐

  1. windo phone8.1 样式的基本使用(一)

    样式的基本使用(一) 当一个项目中有多个控件出现相同的属性设置,那么可以使用以下解决办法 方法一: <Page.Resources> <!--向资源字典中添加一个键为Buttongr ...

  2. win7 打印机共享

    1.在工具->文件夹选项->查看,将"使用简单文件共享"前面的勾勾去掉2.在控制面板->用户帐号,将guest帐户启用3.运行"gpedit.msc&q ...

  3. Oracle的优化器介绍

    Oracle优化器介绍 本文讲述了Oracle优化器的概念.工作原理和使用方法,兼顾了Oracle8i.9i以及最新的10g三个版本.理解本文将有助于您更好的更有效的进行SQL优化工作. RBO优化器 ...

  4. cookie编码乱码问题与cookie禁用后session操作

    Cookie传输的值只能是ASCII码,该编码表相对老旧不含有很多符号与文字 特别是中文,所以在cookie传值过程中需要先转成相应的ASCII编码再解析 如下 URLEncoder.encode(& ...

  5. Sharepoint页面项目展示画廊纯前端实现,后端用list/library简单维护

    需求背景: Sharepoint页面项目展示画廊.图片+文字,要求图片与文字用Sharepoint Library维护,然后在sharepoint页面上被调用,生成项目展示画廊. 解决方案(纯前端), ...

  6. jquery取值

    1. 如何用jquery获取<input id="test" name="test" type="text"/>中输入的值?$( ...

  7. MYSQL trigger 个人记录

    同学要写个trigger,稍微帮他研究了下 以下主要是个人猜想理解的 主要讲update 我们在创建trigger时,数据库会对创建了trigger的表进行事件监听. 当表中的一条记录发生update ...

  8. Leetcode Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  9. 【BZOJ】3561: DZY Loves Math VI

    题意 求\(\sum_{i=1}^{n} \sum_{j=1}^{m} lcm(i, j)^{gcd(i, j)}\)(\(n, m<=500000\)) 分析 很显然要死推莫比乌斯 题解 设\ ...

  10. ZeroMQ接口函数之 :zmq_errno – 返回errno的值给调用此函数的线程

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_errno zmq_errno(3)         ØMQ Manual - ØMQ/3.2.5 Name zm ...