/*
用位操作,乘2相当于左移1位,所以2的幂只有最高位是1
所以问题就是判断你是不是只有最高位是1,怎判断呢
这些数-1后形成的数,除了最高位,后边都是1,如果n&n-1就可以判断了
如果是2的幂,&的结果是全0
*/
if (n<=0) return false;
return ((n&(n-1))==0);

划重点:

一个数*2,出相当于左移一位

2.判断是不是3的幂,没啥用的一道题

public boolean isPowerOfThree(int n) {
/*
不能用循环和递归 感觉不看答案是做不出来
3的幂都可以被int中最大的3的倍数整数
*/
return n > 0 && (1162261467 % n) == 0;
}

3.判断是不是4的幂

重点就是记住:0x55555555,8个5这个16进制的二进制是:奇数位1,偶数位0,用于提取出一个数的奇数位

/*
2的幂中有些不是4的幂,哪些呢?多写几个不难发现
2的幂特征是:最高位是1,后边的是0
而4的幂除了上边的特征,还有最高位必须是从后边数第奇数位
比如8的二进制是1000,从后边数第4位是最高位,不是4的幂,同理还有32
判断最高位是不是偶数位,用0x55555555&上就行,因为这个数的二进制表示
是奇数位为1,偶数位为0
*/
public boolean isPowerOfFour(int num) {
return num>0&(num&(num-1))==0&(num&0x55555555)!=0;
}

[LeetCode]231. Power of Two判断是不是2\3\4的幂的更多相关文章

  1. LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )

    1.题目:原题链接 Given an integer, write a function to determine if it is a power of two. 给定一个整数,判断该整数是否是2的 ...

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

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

  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. LN : leetcode 231 Power of Two

    lc 231 Power of Two 231 Power of Two Given an integer, write a function to determine if it is a powe ...

  5. LeetCode 231 Power of Two

    Problem: Given an integer, write a function to determine if it is a power of two. Summary: 判断一个数n是不是 ...

  6. Leetcode 231 Power of Two 数论

    同样是判断数是否是2的n次幂,同 Power of three class Solution { public: bool isPowerOfTwo(int n) { ) && ((( ...

  7. Java [Leetcode 231]Power of Two

    题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特 ...

  8. [LeetCode] 231. Power of Two ☆(是否2 的幂)

    描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...

  9. (easy)LeetCode 231.Power of Two

    Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @ ...

随机推荐

  1. CenOS下载离线依赖包神器--yumdownloader

    Blog:博客园 个人 本文只总结一些常用的用法,更详细的说明见man yumdownloader和 info yumdownloader. 概述 有时候所在服务器环境不能访问外网,yum安装会感到非 ...

  2. linux scp 命令使用

    1.scp命令使用 linux 把文件复制到另一台服务器上 复制文件 scp file_name user_name@remote_ip:file_path 复制文件夹 scp -r file_nam ...

  3. pom文件中<dependencies>和<dependencyManagement>的区别

    在父pom中,如果使用了<dependencies>标签,那么在该标签体中的所有jar包,即使子工程中没有写这些依赖,依旧会引用. 如果使用了<dependencyManagemen ...

  4. java并发编程实战《七》安全性、活跃性以及性能问题

    安全性.活跃性以及性能问题 安全性问题 那什么是线程安全呢?其实本质上就是正确性,而正确性的含义就是程序按照我们期望的执行,不要让我们感到意外. 存在共享数据并且该数据会发生变化,通俗地讲就是有多个线 ...

  5. Gitlab勾选Remove Source Branch后本地仍然能够看到该分支

    现象: Gitlab合并Merge Request时,勾选了Remove Source Branch,但是本地仍然能够看到该分支(git branch -a),而远程仓库中该分支已经不存在. 解决: ...

  6. SUCTF pythonigx

    0x00知识点 nginx配置 配置文件存放目录:/etc/nginx主配置文件:/etc/nginx/conf/nginx.conf管理脚本:/usr/lib64/systemd/system/ng ...

  7. 在 GitHub 玩硬件——GitHub 热点速览 Vol.49

    作者:HelloGitHub-小鱼干 本周的 GitHub Trending 可以说是非常之硬核,天才少年稚晖君的 2 个硬件变装项目荣登热点榜,看完将充电宝改装为显示器的视频,搭配 HDMI-PI ...

  8. linux 的bash 和 反斜杠

    export BERT_BASE_DIR=/path/to/bert/uncased_L-12_H-768_A-12 export GLUE_DIR=/path/to/glue python run_ ...

  9. matplotlib 绘制多个图——两种方法

    import numpy as np import matplotlib.pyplot as plt #创建自变量数bai组du x= np.linspace(0,2*np.pi,500) #创建函数 ...

  10. 通过url传递对象

    1.使用 encodeURI() 对对象进行编码 2.在另一个页面使用