题目:

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?

思路:

  • 题意是判断一个32位的符号整数是不是4的次方
  • 对于2的次方的判断是n&(n-1)== 0

    10 => 2

    100 => 4

    1000 => 8

    10000 => 16

    100000 => 32

    1000000 => 64

    10000000 => 128

    100000000 => 256

    1000000000 => 512

    10000000000 => 1024

    100000000000 => 2048

    1000000000000 => 4096

    10000000000000 => 8192

    100000000000000 => 16384

    由图中观察可以看出来,4的次方,1都在从右往左数的奇数位,1,3,5等

    所有从2的次方移除4的次方,与上01010101010101010101010101010101,十六进制是0x555555555

代码:

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

LeetCode(65)-Power of Four的更多相关文章

  1. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

  2. C#版 - Leetcode 65. 有效数字 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

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

    Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...

  4. [LeetCode] 342. Power of Four 4的次方数

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

  5. leetcode:Power of Two

    Given an integer, write a function to determine if it is a power of two. 分析:这道题让我们判断一个数是否为2的次方数(而且要求 ...

  6. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  7. LeetCode 342. Power of Four (4的次方)

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

  8. [LeetCode] Reordered Power of 2 重新排序为2的倍数

    Starting with a positive integer N, we reorder the digits in any order (including the original order ...

  9. [LeetCode] 326. 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 ...

随机推荐

  1. java的properties文件-jdbc优化编程(五)

    通过配置文件能够减小我们的工作量,带来方便. 建立properties文件 1.首先是新建一个dbconfig.properties.然后添加如下代码: driver=com.mysql.jdbc.D ...

  2. ToolBar与AppcompatAcitivity实现浸入式Statusbar效果

    toolbar是android sdk API21新增的组件,下面是谷歌官方的介绍文档: A standard toolbar for use within application content. ...

  3. android RecycleView Adapter简单封装

    早些时候我们使用系统提供个的BaseAdapter的时候为了满足大家的需要,我们总会对BaseAdapter做一层上层的封装,然后对于实际业务我们只需要关心getView里面的View即可,是代码可读 ...

  4. Android开发学习之路--传感器之初体验

    说到传感器,还是有很多的,有加速度啊,光照啊,磁传感器等等.当然android手机之所以称为智能手机,少不了这几款传感器的功劳了.下面就学习下了,这里主要学习光照,加速度和磁. 新建工程emSenso ...

  5. Objc将数据写入iOS真机的plist文件中

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如何写入模拟器的博文在 这里 但是这对真机不管用,因为在真机环 ...

  6. x264 n-th pass编码时候Stats文件的含义

    x264 n-th pass(一般是2pass)编码时所用的文件包括下述x264参数生成.stats文件 options: 1280x816 fps=2997/125 timebase=125/299 ...

  7. flex 强制转换类型失败无法将object转换为XXX

    错误描述 flex在加载module时报出如题所示的错误, 实际表现 问题就出现在这 我取消这个错误提示框 再次在前台查询数据 就一切ok 问题就出现在这一句 var zoufangModel:ZfR ...

  8. protobuf代码生成

    windows : 1,两个文件:proto.exe, protobuf-java-2.4.1.jar 2,建立一个工程TestPb,在下面建立一个proto文件件,用来存放[.proto]文件 3, ...

  9. Linux 句柄是什么 ?

    1.句柄就是一个标识符,只要获得对象的句柄,我们就可以对对象进行任意的操作. 2.句柄不是指针,操作系统用句柄可以找到一块内存,这个句柄可能是标识符,map的key,也可能是指针,看操作系统怎么处理的 ...

  10. ACM竞赛:立方和问题

    例如: 输入: n 代表多组数组 num1 , num 2 ep:  1    3 这时的算法结果应当为: 1 ^ 3 + 2 ^ 3 + 3 ^ 3 ep : 2    5 这时的算法结果应当为: ...