【Leetcode Algorithm】Power of Two
来源:互联网 发布:软件体系架构 pdf 编辑:程序博客网 时间:2024/06/05 16:20
Given an integer, write a function to determine if it is a power of two.
第一次尝试代码:
public class Solution { public boolean isPowerOfTwo(int n) { //如果为0,则false if(n==0){ return false; } //2^0=1 if(n==1){ return true; } //n为奇数则肯定不是,若是偶数则递归解决 if(n%2!=0){ return false; } else{ Solution s = new Solution(); return s.isPowerOfTwo(n/2); } }}
第二次尝试代码:
public class Solution { public boolean isPowerOfTwo(int n) { //负数与0都不是 if(n<=0){ return false; } //如果n与n-1与运算之后不为0,则表明n的二进制数1的个数大于1,则判定肯定不是 else if((n&(n-1))!=0){ return false; } else{ return true; } }}
0 0
- 【Leetcode Algorithm】Power of Two
- [LeetCode] Power of Two
- 【LeetCode】Power of Two
- [Leetcode]Power of Two
- [leetCode] Power of Two
- leetcode --Power of Two
- leetcode: Power of Two
- LeetCode Power of Two
- [LeetCode] Power of Two
- LeetCode Power of Two
- [leetcode] Power of Two
- [Leetcode]Power of Two
- Leetcode: Power of Two
- [LeetCode]Power of Two
- leetcode | Power of Two
- 【leetcode】Power of Two
- [LeetCode]Power of Two
- [leetcode] Power of two
- Qt部件----按钮,标签,表的使用
- android 百分比控件的使用
- spring动画(swift)
- 黑马程序员——Java基础---集合(第18天 )
- S3C2410按键poll
- 【Leetcode Algorithm】Power of Two
- Android 增强版百分比布局库 为了适配而扩展
- 数据在计算机中的存储形式和运算( 原码,反码,补码)&=, |=, >>=, <<=, ∧=
- 查找(一)静态表查找
- Mali GPU OpenGL ES 应用性能优化--测试+定位+优化流程
- "n\x45y\123\t"长度
- Insert Interval Array
- Android 进阶学习:Android自定义View的实现方法,带你一步步深入了解View(四)
- Xcode_缓存清理