# leetcode解题报告233——Power of Two

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

package bitmanipulation;

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

public class PowerofTwo {

public boolean isPowerOfTwo(int n) {
if (n <= 0) {
return false;
}
String s = Integer.toBinaryString(n);
if (s.charAt(0) == '0') {
return false;
} else {
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == '1') {
return false;
}
}
}
return true;
}

public boolean isPowerOfTwo2(int n) {
if (n <= 0) {
return false;
}
while (n > 0) {
if (n != 1 && n % 2 == 1) {
return false;
}
n = n >> 1;
}
return true;
}

public boolean isPowerOfTwo3(int n) {
if (n <= 0) {
return false;
}
return (n & (n - 1)) == 0;
}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new PowerofTwo().isPowerOfTwo3(16));
}

}

• 本文已收录于以下专栏：

## LeetCode 231. Power of Two 解题报告

LeetCode 231. Power of Two 解题报告

## LeetCode 326. Power of Three 解题报告

LeetCode 326. Power of Three 解题报告

## LeetCode 350. Intersection of Two Array II 解题报告

LeetCode 350. Intersection of Two Array II 解题报告

## [LeetCode] Median of Two Sorted Arrays 解题报告

—— write for my baby, mua [题目] There are two sorted arrays nums1 and nums2 of size m and n respe...

## UVA 113 Power of Cryptography 解题报告

Power of Cryptography  Background Current work in cryptography involves (among other things) l...

## LeetCode 1.Two Sum 解题报告

LeetCode 1. Two Sum 解题报告

## Leetcode (1) Two Sum 解题报告

Leetcode (1) Two Sum 在一个数组中找到两个数，使其和为目标值，返回下标。

## LeetCode解题报告 1. Two Sum［easy]

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)