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

Example 1:

  1. Input: 1
  2. Output: true

Example 2:

  1. Input: 16
  2. Output: true

Example 3:

  1. Input: 218
  2. 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:

  1. public boolean isPowerOfTwo(int n) {
  2. if(n<=0)
  3. return false;
  4.  
  5. while(n>2){
  6. int t = n>>1;
  7. int c = t<<1;
  8.  
  9. if(n-c != 0)
  10. return false;
  11.  
  12. n = n>>1;
  13. }
  14.  
  15. return true;
  16. }  

Java:

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

Java:

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

Python:

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

Python:

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

C++:

  1. class Solution {
  2. public:
  3. bool isPowerOfTwo(int n) {
  4. int cnt = 0;
  5. while (n > 0) {
  6. cnt += (n & 1);
  7. n >>= 1;
  8. }
  9. return cnt == 1;
  10. }
  11. };  

C++:

  1. class Solution {
  2. public:
  3. bool isPowerOfTwo(int n) {
  4. return (n > 0) && (!(n & (n - 1)));
  5. }
  6. };

  

类似题目:

[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. element ui 中的 resetFields() 报错'resetFields' of undefined

    每次做各种form表单时,首先要注意的是初始化,但是刚开始若没有仔细看文档,则会自己写个方法将数据设置为空,但是这样就会出现一个问题,表单内存在各种验证,假如是一个弹框内有form表单,弹框出现就执行 ...

  2. js Date对象和数字对象

    <script type="text/javascript"> alert(new Date.toLocaleString()); </script> 以本 ...

  3. HDU - 3535:AreYouBusy (分组背包)

    题意:给你n个工作集合,给你T的时间去做它们.给你m和s,说明这个工作集合有m件事可以做,它们是s类的工作集合(s=0,1,2,s=0说明这m件事中最少得做一件,s=1说明这m件事中最多只能做一件,s ...

  4. 用Queue控制python多线程并发数量

    python多线程如果不进行并发数量控制,在启动线程数量多到一定程度后,会造成线程无法启动的错误. 下面介绍用Queue控制多线程并发数量的方法(python3). # -*- coding: utf ...

  5. 简述Python的深浅拷贝以及应用场景?

    浅拷贝:copy.copy                       深拷贝:copy.deepcopy 浅拷贝指仅仅拷贝数据集合的第一层数据,深拷贝指拷贝数据集合的所有层 主要应用在字符串,数字的 ...

  6. vue基本使用及脚手架使用

    一.基本使用: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. jsp解决大文件断点续传

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...

  8. 集成omnibus-ctl 开发一个专业的软件包管理工具

    前边有转发过来自chef 团队的一篇omnibus-ctl 介绍文章,以下尝试进行项目试用 就是简单的集成,没有多少复杂的操作 环境准备 ruby ruby 使用2.6.3 使用 rbenv 安装,可 ...

  9. cube.js 学习 cube 连接mongodb 试用

    cube.js 对于mongodb 的连接是通过mongodb bi connector(mysql 协议)处理的,以下为简单的试用 安装mongo bi connector 这个玩意用docker ...

  10. SPA 首屏加载性能优化之 vue-cli3 拆包配置

    前言 现在已经是vue-cli3.x    webpack4.x 的时代了,但是网上很多拆包配置还是一些比较低版本的. 本文主要是分享自己的拆包踩坑经验. 主要是用了webpack4 的 splitC ...