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

版权声明:本文为博主原创文章,转载请附上原文链接! https://blog.csdn.net/qq_22770457/article/details/53583605

原题地址: 

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;
			}
 	    }
}


猜你喜欢

转载自blog.csdn.net/qq_22770457/article/details/53583605