题目:

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. nginx服务器绑定域名和设置根目录

    首先进入nginx安装目录的配置目录conf,然后执行 vi conf/nginx.conf 打开nginx的配置文件,找到并修改红字部分 server { listen default_server ...

  2. spring多数据源配置

    项目中我们经常会遇到多数据源的问题,尤其是数据同步或定时任务等项目更是如此.多数据源让人最头痛的,不是配置多个数据源,而是如何能灵活动态的切换数据源.例如在一个spring和hibernate的框架的 ...

  3. android LayoutInflater.inflate()的参数介绍

    LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...

  4. ios开发--旋转、移动、缩放手势实例代码

    代码如下: // 添加所有的手势 - (void) addGestureRecognizerToView:(UIView *)view { // 旋转手势 UIRotationGestureRecog ...

  5. 用于主题检测的临时日志(d94169f9-f1c0-45a2-82d4-6edc4bd35539 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    这是一个未删除的临时日志.请手动删除它.(5327dce0-d2d1-4fba-8801-d3ff67564a96 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

  6. ios状态栏调整 简单动画的知识点

    首先状态栏式view的属性,所以在viewController中直接重写: /** 修改状态栏 */ - (UIStatusBarStyle)preferredStatusBarStyle { // ...

  7. JAVA里的String、Timestamp、Date相互转换(转)

    转自:http://blog.sina.com.cn/s/blog_6675493d0100lbfl.html Timestamp转化为String: SimpleDateFormat df = ne ...

  8. HTML5 本地裁剪图片

    下面奉上我自己写的一个demo,代码写得比较少,很多细节不会处理.如果有不得当的地方恳请指教,谢谢啦 ^_^ ^_^   功能实现步奏:   一:获取文件,读取文件并生成url   二:根据容器的大小 ...

  9. ZOJ Monthly, August 2014

    A Abs Problem http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5330 找规律题,构造出解.copyright@ts ...

  10. hlsl 的tex函数

    texCUBE http://msdn.microsoft.com/en-us/library/windows/desktop/bb509687(v=vs.85).aspx