LeetCode每日一题——231. Power of Two

原题地址: 

https://leetcode.com/problems/power-of-two/


Fizz Buzz

描述

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


举例

解题思路

判断是否是2的次方数,并进行di'gu


作答

//判断2的次方数
public class PowerofTwo {
	public static void main(String[] args) {
		for (int i = 0; i < 1000; i++) {
			if (isPowerOfTwo(i)) {
				System.out.println(i);
			}
 		}
	}
	
	static int num = 0;
	public static boolean isPowerOfTwo(double n) {
 			 if (Math.pow(2, num) == n) {
 				 num = 0;
				return true;
			} else if (Math.pow(2, num) < n) {
				num++;
				return isPowerOfTwo(n);
			} else {
				 num = 0;
				return false;
			}
 	    }
}


你可能感兴趣的:(LeetCode)