Given an integer, write a function to determine if it is a power of two.
Example 1:
Input: 1Output: trueExplanation: 20 = 1
Example 2:
Input: 16Output: trueExplanation: 24 = 16
Example 3:
Input: 218Output: false
题意
判断一个整数是不是2的幂。
思路
见代码。
代码实现
class Solution {public boolean isPowerOfTwo(int n) {if (n <= 0) {return false;}while (n % 2 == 0) {n /= 2;}return n == 1;}}
