leetcode Power of Two位运算
2015-07-06 11:27
204 查看
Given an integer, write a function to determine if it is a power of two.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Show Tags
可以直接做:
也可以用位运算:
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Show Tags
可以直接做:
class Solution { public: bool isPowerOfTwo(int n) { if (n < 1) return false; int res; while (n>0) { res = n % 2; if (res != 0) { if (n == 1) return true; else return false; } n = n / 2; } } };
也可以用位运算:
class Solution { public: bool isPowerOfTwo(int n) { if (n < 1) return false; return !(n&(n-1)); } };
相关文章推荐
- XGBoost:在Python中使用XGBoost
- linux程序设计——用互斥量进行同步(第十二章)
- Selenium 2.0 WebDriver 使用指南
- Pro Android学习笔记(一七零):发布应用(3):打开商店、许可和ProGuard
- 查询计划Hash和查询Hash
- yii中常用路径
- 抛弃vboot不格盘用grub4dos+firadisk安装Ghost版XP到VHD,轻松RAMOS!
- 小韦XPSP3 V10.0_Ghost精简版
- 大牛的博客 osharp以及EF的分析
- 整型反序
- EGOCache如何检测缓存时间过期
- Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory。
- 安卓获取手机屏幕尺寸、密度
- 根据圆的面积公式计算圆周率
- 执行计划的重用
- 显示win7桌面网络.reg
- BeagleBone Black教程之BeagleBone Black设备的连接
- UVa 10474 Where is the Marble?
- VTK_Learning_Day_2_Add_Texture
- KNN算法理解