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

Example 1:

Input: 1
Output: true

Example 2:

Input: 16
Output: true

Example 3:

Input: 218
Output: false

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

利用计算机用的是二进制的特点,用位操作,此题变得很简单。

2的n次方的特点是:二进制表示中最高位是1,其它位是0,

1  2   4     8     16    ....

1 10 100 1000 10000 ....

解法:位操作(Bit Operation),用右移操作,依次判断每一位的值,如果只有最高位是1,其余位都是0,则为2的次方数。

解法2: 位操作(Bit Operation),原数减1,则最高位为0,其余各位都变为1,把两数相与,就会得到0。

解法3: 用数学函数log

Java:

public boolean isPowerOfTwo(int n) {
if(n<=0)
return false; while(n>2){
int t = n>>1;
int c = t<<1; if(n-c != 0)
return false; n = n>>1;
} return true;
}  

Java:

public boolean isPowerOfTwo(int n) {
return n>0 && (n&n-1)==0;
}  

Java:

public boolean isPowerOfTwo(int n) {
return n>0 && n==Math.pow(2, Math.round(Math.log(n)/Math.log(2)));
} 

Python:

class Solution:
# @param {integer} n
# @return {boolean}
def isPowerOfTwo(self, n):
return n > 0 and (n & (n - 1)) == 0

Python:

class Solution2:
# @param {integer} n
# @return {boolean}
def isPowerOfTwo(self, n):
return n > 0 and (n & ~-n) == 0  

C++:

class Solution {
public:
bool isPowerOfTwo(int n) {
int cnt = 0;
while (n > 0) {
cnt += (n & 1);
n >>= 1;
}
return cnt == 1;
}
};  

C++:

class Solution {
public:
bool isPowerOfTwo(int n) {
return (n > 0) && (!(n & (n - 1)));
}
};

  

类似题目:

[LeetCode] Number of 1 Bits

[LeetCode] Power of Four

[LeetCode] 326. Power of Three

All LeetCode Questions List 题目汇总

[LeetCode] 231. Power of Two 2的次方数的更多相关文章

  1. [LeetCode] 342. Power of Four 4的次方数

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

  2. [LeetCode] 326. Power of Three 3的次方数

    Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...

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

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

  4. [LeetCode] 231. Power of Two ☆(是否2 的幂)

    描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...

  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. Leetcode 231 Power of Two 数论

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

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

  9. Java [Leetcode 231]Power of Two

    题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特 ...

随机推荐

  1. danci6

    current 英 ['kʌr(ə)nt] 美 ['kɝənt] adj. 现在的:流通的,通用的:最近的:草写的 n. (水,气,电)流:趋势:涌流 n. (Current)人名:(英)柯伦特

  2. 如何有效使用Project(2)——进度计划的执行与监控

    继上次的的<编制进度计划.保存基准>继续讲解如何对计划进行执行和监控. 计划执行即:反馈实际进度.反馈工作消耗(本文只考虑工时,不考虑成本).提出计划变更请求.如果你的企业实施了专门的PM ...

  3. php导出表格两种方法 ——PhpExcel的列子

    php常用的导出表格有两种方法,第一种是输出表格,这种方法打开的时候有警告提示,一般导出表格会用phpexcel,这个导出比较灵活,而且还可以设置表格的样式. 第一种导出例子 /** * 执行导出 * ...

  4. SQL中的trim函数

    Oracle TRIM函数是很常见的函数,下面对Oracle TRIM函数的语法作了详尽的阐述说明,希望可以让您对Oracle TRIM函数有更深的认识. 如果提到Oracle TRIM函数,最简单的 ...

  5. Linux下串口操作

    一.Linux下访问串口 串口位置:/dev/tty** 在Linux系统中,串口设备是通过串口终端设备文件来访问的,也就是通过访问/dev/ttyS0./dev/ttyS1./dev/ttyS2./ ...

  6. WinDbg常用命令系列---显示当前异常处理程序链!exchain

    !exchain 这个!exchain扩展命令显示当前异常处理程序链. !exchain [Options] 参数: Options下列值之一: /c  如果检测到异常,则显示与调试C++ try/c ...

  7. CSS样式表书写位置

    一.内嵌式写法:样式只作用于当前文件,没有真正实现结构表现分离. <head> <style type=”text/css”> 样式表写法 </style> < ...

  8. [NOI2019]回家路线

    [NOI2019]回家路线 题目大意: 有\(n\)个站点,\(m\)趟车,每趟车在\(p_i\)时从\(x_i\)出发,\(q_i\)时到达\(y_i\). 若小猫共乘坐了\(k\)班列车,依次乘坐 ...

  9. 最近在弄ionic3的时候遇到的一些问题(遇到就更新)

    问题一(install提示errno -4048 和管理员权限) npm install npm ERR! code EPERM npm ERR! errno - npm ERR! syscall u ...

  10. (10)Go结构体struct

    结构体 Go语言中的基础数据类型可以表示一些事物的基本属性,但是当我们想表达一个事物的全部或部分属性时,这时候再用单一的基本数据类型明显就无法满足需求了,Go语言提供了一种自定义数据类型,可以封装多个 ...