描述

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. Algorithm 算法基础知识(未完成

    基础概念不讲,记录课上关键部分 时间复杂度(Time Complexity) 算法所需要花的时间 比较时间复杂度(主要看问题的规模) 时间频度(算法执行次数)T(n)和T(n1),如果两个时间频度为等 ...

  2. 力扣(LeetCode) 14. 最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  3. LRN(local response normalization--局部响应标准化)

    LRN全称为Local Response Normalization,即局部响应归一化层,LRN函数类似DROPOUT和数据增强作为relu激励之后防止数据过拟合而提出的一种处理方法.这个函数很少使用 ...

  4. unbuntu安装Node.js

    在官网https://nodejs.org/en/下载 手动创建链接的话,新安装的angular的ng   typescript的tsc都要自己手动建立软链接,要不就每个工程里npm install一 ...

  5. UCS2编码

    UCS2就是标准的unicode编码, 它是某国际组织设计的一种文字符号编码表,包括了世界上绝大多数文字和符号,包括中文,每个字符使用2字节编码,因此叫ucs2. 这里有一篇文章对Unicode编码做 ...

  6. 关于Python中的ifelse、while和for循环的简单小结

    1.ifelse 1.1首先简单编辑一个关于ifelse的程序: _username = 'yanfeixu' _password = 'wuyifan' username = input(" ...

  7. 决策论 | 信息论 | decision theory | information theory

    参考: 模式识别与机器学习(一):概率论.决策论.信息论 Decision Theory - Principles and Approaches 英文图书 What are the best begi ...

  8. PaaS平台型IT运维&运营模式能给企业带来什么?

    关注嘉为科技,获取运维新知 什么是PaaS平台型IT自动化运维&运营模式 PaaS平台型IT运维和运维模式是指:将通用的运维能力与具体的运维场景解耦合,将能够复用的,具备独立功能的通用能力纳入 ...

  9. linux中ping IP可以通但是主机名不通

    在/etc/hosts中配置要ping的主机名称映射 例如: ping test1不通 在/etc/hosts中配置 ip地址  test1

  10. 分离vue组件内部css

    当我们使用vue组件的时候,使用webpack打包的时候,默认会把vue组件内部的css打包到页面上,但是打包到页面上很丑陋,所以我们希望可以把vue组件内部的css抽离到css文件中,使用vue-s ...