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

来源:互联网 发布:如何看待奚梦瑶 知乎 编辑:程序博客网 时间:2024/06/06 10:49

原题地址: 

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


1 0