描述

Given an integer, write a function to determine if it is a power of two.

给定一个整数,编写一个函数来判断它是否是 2 的幂次方。

解析

2的幂只有1个1

仔细观察,可以看出 2 的次方数都只有一个 1 ,剩下的都是 0 。根据这个特点,只需要每次判断最低位是否为 1 ,然后向右移位,最后统计 1 的个数即可判断是否是 2 的次方数。

减一法

如果一个数是 2 的次方数的话,那么它的二进数必然是最高位为1,其它都为 0 ,那么如果此时我们减 1 的话,则最高位会降一位,其余为 0 的位现在都为变为 1,那么我们把两数相与,就会得到 0。

比如 2 的 3 次方为 8,二进制位 1000 ,那么 8 - 1 = 7,其中 7 的二进制位 0111。

正、负相与

4的二进制100。

-4的二进制为4的补码。即取反+1。

先对 00000000 00000000 00000100取反后是11111111 11111111 11111111 11111011,取反后加1得11111111 11111111 11111111 11111100,正是最后结果。

相与&,还是4。

代码

class Solution {
public boolean isPowerOfTwo(int n) {
int cnt = 0;
while (n > 0) {
cnt += (n & 1);
n >>= 1;
}
return cnt == 1;
}
}
class Solution {
public boolean isPowerOfTwo(int n) {
if (n <= 0) {
return false;
}
return (n & (n - 1)) == 0;
}
}
class Solution {
public boolean isPowerOfTwo(int n) {
if (n <= 0) {
return false;
}
return n == (n & -n);
}
}

[LeetCode] 231. Power of Two ☆(是否2 的幂)的更多相关文章

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

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

  2. 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 ...

  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 231 Power of Two

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

  5. Leetcode 231 Power of Two 数论

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

  6. (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 @ ...

  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的n次幂 - 位运算应用实例 - ( C++ )

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

  9. leetcode 231 Power of Two(位运算)

    Given an integer, write a function to determine if it is a power of two. 题解:一次一次除2来做的话,效率低.所以使用位运算的方 ...

随机推荐

  1. 创建react项目

    npm搭建React项目 React官网提供最简便的方法是使用create-react-app npx create-react-app my-app cd my-app npm start 也可以自 ...

  2. 草珊瑚的redux使用方式

    前言 阮大师写入门教程能力一流. 首推它的Redux三篇入门文章. http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_bas ...

  3. Python 基础 Python是什么

    1.Python 是一门高级的.面向对象的,解释性,脚本语言.

  4. vs项目的属性页面总结

    本文主要针对vs中属性页面的相关选项含义进行了总结.

  5. android studio 的基本使用和建立一个小项目

    https://github.com/allenxieyusheng/Android-Studio

  6. tarjan 缩点(模板)

    描述: 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 注:允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 思路: ...

  7. 一: vue的基本使用

    一: vue的下载 vue.js是目前前端web开发最流行的工具库之一,由尤雨溪在2014年2月发布的. 另外几个常见的工具库:react.js /angular.js 官方网站: ​ 中文:http ...

  8. DjangoBlog安装

    DjangoBlog安装 下载 https://github.com/liangliangyy/DjangoBlog/archive/v7.0.tar.gz pip install -Ur requi ...

  9. java开发手册(阿里巴巴)——编程规约(部分)

    (一)命名风格 3. [强制]类名使用 UpperCamelCase 风格,但以下情形例外:DO / BO / DTO / VO / AO / PO / UID 等. 正例:MarcoPolo / U ...

  10. Android自定义权限

    一.自定义权限 自定义权限,一般是考虑到应用共享组件时的安全问题.我们知道在四大组件 AndroidManifest 中注册的时候,添加 exported = "true" 这一属 ...