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. WPF中的数据绑定!!!

    引用自:https://msdn.microsoft.com/zh-cn/magazine/cc163299.aspx  数据点: WPF 中的数据绑定 数据点 WPF 中的数据绑定 John Pap ...

  2. mysql 总结二(自定义函数)

    本质:mysql内置函数的一种扩展,本质上与mysql内置函数一样. 函数必要条件: @1:参数(非必备): @2:返回值: 模板: create function function_name ret ...

  3. C# 中excel操作

    c#中设置Excel单元格格式    1.全表自动列宽 mysheet.Cells.Select(); mysheet.Cells.Columns.AutoFit(); 2.合并    excelRa ...

  4. LUXURY 8

    A - Gargari and Bishops Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  5. android Activity基类通用方法

    public class BaseActivity extends Activity { Resources res; // 通用资源缩写 @Override protected void onCre ...

  6. 剑指Offer 二叉树中和为某一值的路径(dfs)

    题目描述 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.     思路: 递归,然后深搜,因为题目定义的, ...

  7. 2015校招网易C/C++工程师笔试题(附答案)

    1. #include < filename.h >和#i nclude “filename.h” 有什么区别?   答:对于#i nclude < filename.h >, ...

  8. BZOJ 4455: [Zjoi2016]小星星

    Sol 容斥原理+树形DP. 这道题用的容斥思想非常妙啊!主要的思路就是让所有点与S集合中的点对应,可以重复对应,并且可以不用对应完全(意思是是S的子集也可以).这样他有未对应完全的,那就减去,从全都 ...

  9. centos6.5 网卡的处理

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

  10. ubuntu14.04 server 安装vmware worktation 12

    0) Do the basic system installation of Ubuntu 14.04 LTS (Server or Desktop) 1) wget the installer wg ...