231. Power of Two
来源:互联网 发布:金数据登录 编辑:程序博客网 时间:2024/05/18 17:03
Given an integer, write a function to determine if it is a power of two.
My solution:
public class Solution { public boolean isPowerOfTwo(int n) { if(n<1) return false; if((n&(n-1))==0) return true; else return false; }}
Or use Integer.bitCount.
public class Solution { public boolean isPowerOfTwo(int n) { return n>0&&Integer.bitCount(n)==1; }}
0 0
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- 231. Power of Two
- Objective-C基础
- Toolbar+NavigationView实现抽屉式菜单
- Android中的Shape使用总结
- 计算机基础,数字在计算机中是怎么表示的
- JAVA设计模式---单态模式理解
- 231. Power of Two
- C++动态数组
- 服务评价(星标控件)
- 使用Maven构建多模块项目
- 编程中的常量
- Cookie HttpSession 学习笔记
- CSS:设置文本与字体样式
- 数运算的一些模板
- 利用leaks查找内存泄露(Xcode7.2)