LeetCode 231 Power of Two
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的更多相关文章
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- 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 ...
- [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: ...
- Leetcode 231 Power of Two 数论
同样是判断数是否是2的n次幂,同 Power of three class Solution { public: bool isPowerOfTwo(int n) { ) && ((( ...
- (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 @ ...
- Java [Leetcode 231]Power of Two
题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特 ...
- [LeetCode] 231. Power of Two ☆(是否2 的幂)
描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...
- 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的 ...
- leetcode 231 Power of Two(位运算)
Given an integer, write a function to determine if it is a power of two. 题解:一次一次除2来做的话,效率低.所以使用位运算的方 ...
随机推荐
- QT中Sqlite的使用
环境: 静态编译过sqlite 步骤: 1.C++链接器中加入Sqlite.lib,然后在测试一下是否能正常加载Sqlite驱动 #include<QtPlugin> Q_IMPORT_P ...
- isNaN() 确认是否是数字
isNaN(x): 当变量 x 不是数字,返回 true: 当变量 x 是其他值,(比如,1,2,3),返回false.
- Hadoop 之面试题
颜色区别: 蓝色:hive,橙色:Hbase.黑色hadoop 请简述hadoop怎样实现二级排序. 你认为用Java,Streaming,pipe 方式开发map/reduce,各有哪些优缺点: 6 ...
- 使用批处理(bat)脚本对目录树下同种性质的目录或文件进行处理
问题起源:每次从svn管理的目录下面复制目录之后里面总是有很多.svn的目录,虽说不影响使用但看着很碍眼.同时自己也懒得使用svn的export功能. 因此一个简单的批处理脚本可以帮助我们搞定一切,当 ...
- Lvs原理
官方文档: http://www.linuxvirtualserver.org/zh/lvs1.html http://www.linuxvirtualserver.org/zh/lvs2.html ...
- python 数据结构 初学时没太注意却发现很有用的点点滴滴
1. list.extend(L) 将指定列表中的所有元素附加到另一个列表的末尾:相当于a[len(a):] = L. 2. list.pop([i]) 删除列表中指定位置的元素并返回它.如果未指定索 ...
- Word撤销键(Ctrl+z)无效的解决方法
最近翻译一本新书,Word2013用的较多,于是发现了一个奇怪的问题,撤销按钮一直是灰色.编辑的时候闪一下,又变为灰色.按Ctrl-Z也同样不管用.中文资源里面的解决方法都是用winword.exe ...
- 一起入门python6之函数
今天我们来学习新的一篇吧,那便是“函数(function)”我们用def来定义一个函数,以案例说话.>>> def name(x): #定义一个“name”的函数. ...
- css选择器(E[att^=”val”]序号选择器)
一.基本选择器序号 选择器 含义1. * 通用元素选择器,匹配任何元素2. E 标签选择器,匹配所有使用E标签的元素3. .info class选择器,匹配所有class属性中包含info的元素4. ...
- net-snmp-5.7.3配置编译安装
net-snmp-5.7.3配置编译安装 [TOC] 先看一下系统环境 o@o-pc:~/work/_snmp/net-snmp-5.7.3$ uname -a Linux o-pc 3.16.0-3 ...