标签:power rgs out 进制 ret stat while pre bool
package whale.leetcode.simple;
/**
* @Author: WhaleFall541
* @Date: 2021/5/31 23:22
*/
public class PowerOfFour {
public static void main(String[] args) {
System.out.println(isPowerOfFour1(1073741825));
}
public static boolean isPowerOfFour1(int n) {
if (n == 1)
return true;
int a = 4;
// a还没到n的大小就继续乘以4,最后大小溢出后a=0
while (n > a && a > 0) {
a = a * 4;
}
if (n == a)
return true;
else
return false;
}
public static boolean isPowerOfFour(int n) {
// 此处看的答案 二进制 011 & 100 = 000 而且 4 % 3 =1
return n > 0 && (n & (n - 1)) == 0 && n % 3 == 1;
}
}
标签:power rgs out 进制 ret stat while pre bool
原文地址:https://www.cnblogs.com/whalefall541/p/14835292.html