leetcode--Power of Two
来源:互联网 发布:星际淘宝网588 编辑:程序博客网 时间:2024/06/08 02:30
题目:Power of Two
Given an integer, write a function to determine if it is a power of two.
One:以n为参数 n/2为变化率进行循环Iterable
public class Solution { public boolean isPowerOfTwo(int n) { if(n==1)return true; while(n!=0){ if(n==1)return true; if(n%2!=0)break; n/=2; } return false; }}Two:进一步简化
public class Solution { public boolean isPowerOfTwo(int n) { if(n==1)return true; while(n%2==0){ n/=2; } return n==1; }}
Three:递归的方法Recursive
public class Solution { public boolean isPowerOfTwo(int n) { if(n==1)return true; if(n<1||n%2 !=0)return false; return isPowerOfTwo(n/2); }}
Four:Recursive进一步简化
public class Solution { public boolean isPowerOfTwo(int n) { return n>0 && (n==1 || (n%2==0 && isPowerOfTwo(n/2))); }}
0 0
- [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
- LeetCode Power of Two
- 数据结构--直接插入排序
- Hibernate的数据映射关系
- R-CNN,SPP-NET, Fast-R-CNN,Faster-R-CNN, YOLO, SSD系列深度学习检测方法梳理
- Android开发——倒计时器自定义chronometer
- Spring 配置多数据源实现
- leetcode--Power of Two
- 李飞飞-我们该如何做研究
- 【NOIP提高组】B
- apache静态文件配置
- A【NOIP2017提高组模拟12.18】
- 对pearson相关系数以及协方差矩阵的理解
- 泰勒与极限
- MFC操作注册表
- c++教程(一)c过渡c++