题目:

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. 集合框架之Set接口

    一个不包含重复元素的 collection.更确切地讲,set 不包含满足e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素. 在所有构造方法以及 add.equa ...

  2. JS和Jquery操作label标签

    获取label值:  label标签在JS和Jquery中使用不能像其他标签一样用value获取它的值: 代码如下: var label=document.getElementById("s ...

  3. ceil和floor函数的编程实践

    ceil()向上取整 floor向下取整 题目 在最近几场魔兽争霸赛中,赫柏对自己的表现都不满意. 为了尽快提升战力,赫柏来到了雷鸣交易行并找到了幻兽师格丽,打算让格丽为自己的七阶幻兽升星. 经过漫长 ...

  4. expect 简单使用

    简单的登陆脚本 这样就不用每次都输入ssh命令了,使用密码还是有些不安全,谨慎使用. #!/usr/bin/expect -f #filename: auto_login.sh #author: or ...

  5. 浅谈C语言 extern 指针与数组

    /* * d.c * * Created on: Nov 15, 2011 * Author: root */ #include "apue.h" int a[] = {3,2}; ...

  6. Cocos2D iOS之旅:如何写一个敲地鼠游戏(三):素材最终解决方法

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  7. Android(Lollipop/5.0) Material Design(二) 入门指南

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简介 Android(Lollipop/5.0)Material Design(二) ...

  8. Mybatis事务(三)事务工厂

    在前面一篇博客Mybatis事务(一)事务管理方式中我们提到,mybatis及spring提供了三个事务实现类:JdbcTransaction.ManagedTransaction和SpringMan ...

  9. github管理的建立(SSH Key生成步骤)

    Git是分布式的代码管理工具,远程的代码管理是基于SSH的,所以要使用远程的Git则需要SSH的配置. github的SSH配置如下: 一 . 设置Git的user name和email: $ git ...

  10. Hive 配置

    <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="confi ...