leetcode之Power of Two
2016-04-07 01:15
302 查看
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.
Subscribe to see which companies asked this question;
该题判断一个数是否为2的幂,这里利用的是二进制的方法,如果一个数为2的幂,则只有首位为1,所以与其减一的结果相与的结果为0。
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question;
该题判断一个数是否为2的幂,这里利用的是二进制的方法,如果一个数为2的幂,则只有首位为1,所以与其减一的结果相与的结果为0。
class Solution { public: bool isPowerOfTwo(int n) { if(n>0){ return (!(n&(n-1))); } else return false; } };
相关文章推荐
- 《深入理解计算机系统》第七章 学习总结
- 一位夜深人静后码农的心里独白
- 关于linux系统中的权限
- OSChina 周四乱弹 ——滔哥 在回忆时还是暖暖的
- [Angular 2] *ngFor with index
- swift 之CoreData使用
- Maven中Spring-Data-Redis存储对象(redisTemplate) (转)
- 第六周作业
- naoting
- naoting
- redis作为mysql的缓存服务器(读写分离) (转)
- jQuery的ajax实现登录验证
- 解决 error while loading shared libraries: libghttp.so.1: cannot open shared object file
- 1006. Sign In and Sign Out
- keepalive安装配置
- Unity Shaders and Effects Cookbook (3-2) 创建简单的 Phong 高光 光照模型
- 离线Markdown编辑器推荐
- 斐波那契数列
- document.ready和onload的区别——JavaScript文档加载完成的那点事儿
- 作业四