Leet Code OJ 231. Power of Two [Difficulty: Easy]

来源:互联网 发布:菜鸟驿站 不是淘宝 编辑:程序博客网 时间:2024/05/02 02:22

题目:
Given an integer, write a function to determine if it is a power of two.

分析:
题意是给定一个整数,判断它是不是2的幂。

代码实现:

public class Solution {    public boolean isPowerOfTwo(int n) {        if(n<1){            return false;        }        if(n==1){            return true;        }        if((n&1)==0) {            return isPowerOfTwo(n/2);        } else{            return false;        }    }}
0 0