leetcode 231 Power of Two(位运算)
Given an integer, write a function to determine if it is a power of two.
题解:一次一次除2来做的话,效率低。所以使用位运算的方法(左移一位相当于原数乘2)列出在int范围内的所有2的倍数,然后和n异或如果等于0,那么n就是它(两个数的异或为0,那么两个数就相等)。
注意:位运算的优先级比==的优先级低,要加括弧。
class Solution {
public:
bool isPowerOfTwo(int n) {
int a=;
if(n<=){
return false;
}
else{
for(int i=;i<=;i++){
if((n^a)==){
return true;
}
else{
a<<=;
}
}
return false;
}
}
};
更好的解法:一个数是2的倍数,那么它的二进制表示中只有一个1,其它都是0,所以如果n&(n-1)==0,那么n就是2的倍数(因为减1只会影响二进制表示中最靠右的1).
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. ...
- [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: ...
- 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 78. 子集 C++(位运算和回溯法)
位运算 class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { ...
- 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 ☆(是否2 的幂)
描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...
- LeetCode 231 Power of Two
Problem: Given an integer, write a function to determine if it is a power of two. Summary: 判断一个数n是不是 ...
- 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 @ ...
随机推荐
- JVM调优- jstat(转)
jstat的用法 用以判断JVM是否存在内存问题呢?如何判断JVM垃圾回收是否正常?一般的top指令基本上满足不了这样的需求,因为它主要监控的是总体的系统资源,很难定位到java应用程序. Jstat ...
- python设置环境变量
方法1: 当程序开发完毕,就可以添加到 '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages'中 ...
- MongoDBTemplate多条件查询的问题
问题: 在使用Spring Data MongoDB 进行条件查询数据时,发现条件判断不起作用,结果会返回所有的数据. Criteria criteria = new Criteria(); crit ...
- PopupWindowFromBottom 从底部弹出popupwindow
自定义PopupWindowFromBottom public class PopupWindowFromBottom extends PopupWindow { public PopupWindow ...
- docker安装并配置加速
安装 旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本: sudo apt-get remove docker \ docker-engine \ ...
- CentOS查看和修改MySQL字符集
通过以下命令查看了MySQL的字符集 连接上mysql服务,输入以下命令 mysql>show variables like 'character_set%'; 显示如下: 为了让MySQL支持 ...
- Android LockScreen (锁屏弹窗)
在要弹窗的Activity需要进行以下设置,才可以在锁屏状态下弹窗 @Override protected void onCreate(Bundle savedInstanceState) { fin ...
- C#转换人民币大写金额
/// <summary> /// 转换人民币大写金额. /// </summary> public class RMBConverter { /// <summary& ...
- jQuery水平滑动菜单
在线演示 本地下载
- Google员工自述:在哈佛教书和在Google工作的差别
感谢伯乐在线的投递编者按:2003年到2010年期间,原文作者Matt Welsh 是哈佛大学工程和应用科学学院的计算机科学系教授.2010年加入Google,是一名高级工程师.他当前的工作重点是广域 ...