题目:

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. IOC学习

    控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控制反转一般分为两种类型,依赖注入 ...

  2. js 数组去重复键

    Array.prototype.deleteEle = function() { var newArr = this; for (var i = newArr.length - 1; i >= ...

  3. Spark菜鸟学习营Day5 分布式程序开发

    Spark菜鸟学习营Day5 分布式程序开发 这一章会和我们前面进行的需求分析进行呼应,完成程序的开发. 开发步骤 分布式系统开发是一个复杂的过程,对于复杂过程,我们需要分解为简单步骤的组合. 针对每 ...

  4. Noppoo choc mini 84 @XUbuntu13.10 compatibility setting

    Months ago, I bought the keyboard Noppoo Choc Mini 84keys for using under XUbuntu12.10, and I have f ...

  5. 【Web学习日记】——在IIS上发布一个WebService

    没有开发过程,只是发布过程 一.前提 开发使用的是VS2013 从来没有做过Web的发布,在网上找例子,看到的总是与自己的情况不相符,而且也有人提出了VS2013发布网站的问题,但解决方案却很少,好不 ...

  6. JDBC 连接数据库

          JAVA使用JDBC访问数据库的步骤: 1.     得到数据库驱动程序   (导包) 2.     创建数据库连接  3.     执行SQL语句 4.     得到结果集 5.     ...

  7. cocos2dx中的触摸事件及触摸优先级

    1.只有CCLayer及其派生类才有触摸功能. 2.开启触摸 setTouchEnable(true); 3.设置触摸模式,单点,多点(仅IOS支持) setTouchMode(kCCTouchesO ...

  8. Android 自定义Toast,不使用系统Toast

    效果图: 创建Toast类 package com.example.messageboxtest; import android.app.Activity; import android.conten ...

  9. JSP访问Spring中的bean

    JSP访问Spring中的bean <%@page import="com.sai.comment.po.TSdComment"%> <%@page import ...

  10. Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜

    题目链接: 题目 D. Toy Sum time limit per test:1 second memory limit per test:256 megabytes 问题描述 Little Chr ...