Power of 4
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;
}
}
Agreement
The code part of this work is licensed under Apache License 2.0 . You may freely modify and redistribute the code, and use it for commercial purposes, provided that you comply with the license. However, you are required to:
- Attribution: Retain the original author's signature and code source information in the original and derivative code.
- Preserve License: Retain the Apache 2.0 license file in the original and derivative code.
- Attribution: Give appropriate credit, provide a link to the license, and indicate if changes were made.
- NonCommercial: You may not use the material for commercial purposes. For commercial use, please contact the author.
- ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.