Problem:

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

Summary:

判断一个数n是不是2的整数幂。

Analysis:

这道题首先需要注意n为非整数是返回false的情况。

1. 若将n转换为二进制数,如果n为2的整数幂,则转换得到的二进制数只包含一个一,故计算转换过程中1的个数,最终判断。

 class Solution {
public:
bool isPowerOfTwo(int n) {
int cnt = ; while (n > ) {
if (n % ) {
cnt++;
}
n /= ;
} return cnt == ? true : false;
}
};

可以简化为位操作的形式,如下:

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

2. 经过查网上大牛的代码,学到了一个神奇的技巧:当一个数n为2的整数幂时,其二进制最高位必为1,其余位数都为0。那么n-1最高位为0,其余位数均为1。则若n & (n -1)为0时,n必为2的整数幂。

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

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

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

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

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

  3. [LeetCode] 231. Power of Two 2的次方数

    Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...

  4. Leetcode 231 Power of Two 数论

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

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

  6. Java [Leetcode 231]Power of Two

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

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

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

  8. LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )

    1.题目:原题链接 Given an integer, write a function to determine if it is a power of two. 给定一个整数,判断该整数是否是2的 ...

  9. leetcode 231 Power of Two(位运算)

    Given an integer, write a function to determine if it is a power of two. 题解:一次一次除2来做的话,效率低.所以使用位运算的方 ...

随机推荐

  1. iSCSI配置流程

    Windows群集两个节点:分别在SQL01和SQL02设置连接共享磁盘: 此前已经在存储服务器通过StarWind创建了三个虚拟磁盘:Quemon+data+backup:starwind安装请参考 ...

  2. ios 微博登录 21338错误 sso package or sign error

    os中,是你项目的bundle id 和 连接的微博开发账户里面的bundle id 不一致造成的. 特别是,你把你注册的appKey放到你下载的官方的demo里面测试,肯定会出现这种情况, 所以,你 ...

  3. Effective Java 学习笔记之所有对象都通用的方法

    一.覆盖equals时请遵守通用约定 1.满足下列任何一个条件时,不需要覆盖equals方法 a.类的每个实例本质上都是唯一的.此时就是Object中equals方法所表达的含义. b.不关心类是否提 ...

  4. [codeforces 339]E. Three Swaps

    [codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...

  5. word20161203

    B-channel / B 信道 B-ISDN, broadband integrated services digital network / 广播综合业务数字网络 backbone router  ...

  6. 跟着百度学PHP[4]-OOP面对对象编程-3-实例化一个对象

    当定义好类后,我们使用new关键字来实例化一个对象! 格式: $object = new 类名; <?php class Person{ private $name; "; priva ...

  7. Python 学习笔记二

    笔记二 :print 以及基本文件操作 笔记一已取消置顶链接地址 http://www.cnblogs.com/dzzy/p/5140899.html 暑假只是快速过了一遍python ,现在起开始仔 ...

  8. centos6.5 网卡的处理

    在centos安装后,找不到eth0/1 看了下dmesg | grep network的输出,发现不是驱动没有安装,而是重新命名了网卡. 1. 配置静态IP,修改 /etc/sysconfig/ne ...

  9. IDEA 新建文件默认加入CVS

    是要先add,不过可以设置创建的文件都默认 add的.修改默认值看下图:打开系统设置,找到 Version Control 设置选项: 在 When files are created 选项中选择第二 ...

  10. 向SqlServer数据库插入数据

    Insert Values Insert Select Insert Exec Select Into Bulk Insert Insert Values是最常用的一种插入数据的方式,基本语法如下,表 ...