题目:

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

链接: http://leetcode.com/problems/power-of-two/

题解:

验证一个数是否是2的幂次,我们可以使用(n & (n - 1))来决定。 比如 10 & 01, 100 & 011等等。

Time Complexity - O(1), Space Complexity - O(1)

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

二刷:

二的幂有一个特点,就是整个数字里只有一个比特位为1,其余都是0, 我们可以count有几个比特位为1,大于1的话结果为false,等于1的话为true

Java:

Time Complexity - O(1), Space Complexity - O(1)

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

更简练的写法 - 因为2的幂次只有一个比特位为1,那么我们用n & (n - 1)将其清零后,得到的结果应该是0。

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

Reference:

https://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2

231. Power of Two的更多相关文章

  1. 231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂

    231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...

  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 - 326, 342, 231 Power of Three, Four, and Two

    1. 问题 231. Power of Two: 判断一个整数是否是2的n次方,其中n是非负整数 342. Power of Four: 判断一个整数是否是4的n次方,其中n是非负整数 326. Po ...

  4. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

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

  6. LeetCode 231 Power of Two

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

  7. python leetcode 日记--231. Power of Two

    题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...

  8. Leetcode 231 Power of Two 数论

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

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

  10. 【LeetCode】231 - Power of Two

    Given an integer, write a function to determine if it is a power of two. Solution:一个整数如果是2的整数次方,那么它的 ...

随机推荐

  1. 单例模式C#

    首先来明确一个问题,那就是在某些情况下,有些对象,我们只需要一个就可以了, 比如,一台计算机上可以连好几个打印机,但是这个计算机上的打印程序只能有一个, 这里就可以通过单例模式来避免两个打印作业同时输 ...

  2. EWOULDBLOCK = EAGAIN

    #define EAGAIN 11 /* Try again */ #define EINTR 4 /* Interrupted system call */ #define EWOULDBLOCK ...

  3. mysql5.7.12安装过程中遇到的一些问题

    在安装mysql-5.7.12-winx64中遇到的问题总结 1.该版本的mysql解压后的文件夹里没有data文件(切记自己添加data,自己添加的文件可能出现的问题是文件里的文件会缺失) 我在使用 ...

  4. 无法解决 equal to 运算中 "Chinese_PRC_BIN" 和 "Chinese_PRC_CI_AS" 之间的排序规则冲突

    无法解决 equal to 运算中 "Chinese_PRC_BIN" 和 "Chinese_PRC_CI_AS" 之间的排序规则冲突.问题如下图: 执行一下语 ...

  5. ORA-1653: unable to extend table SYS.AUD$

    今早运维组的同事反映有个系统功能很多地方都报错,怀疑是不是数据库有什么问题.于是登录数据库检查,通过crsctl status res -t检查,发现所有集群资源都是OK的,没有哪个资源挂掉了.于是到 ...

  6. python之else总结

    python中除了if...elif...else..还有while...else, for...else..., try...except...else...finally... 不管哪种else, ...

  7. PHP正则表达式的逆向引用与子模式 php preg_replace应用

    mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit]) 功能 在 subject 中搜索 ...

  8. Windows Media Player安装了却不能播放网页上的视频

    前段时间遇到Windows Media Player安装了却不能播放网页上的视频的问题,在网上查找资料时,发现大部分资料都没能解决我这个问题.偶尔试了网上一牛人的方法,后来竟然解决了.现在再找那个网页 ...

  9. Madwifi Mad coding:自底向上分析associated_sta的更新过程 —— RSSI和MACADDR等信息获取的底层原理

    Madwifi驱动工作在AP模式下时,可以在/proc/net/madwifi/ath0/associated_sta文件中得到所有接入的用户的MAC地址.实时平均RSSI,和last_rx三个信息. ...

  10. Linux 命令整理 —— 基本操作

    1.ls 目录列举(dir) 一般我们这么写: ls 列举当前目录的所有文件,如果文件很多的话,这么看很复杂.我们可以加关键字,例如我们要看包含xml的全部文件. ls *xml* 如果这个时候,我们 ...