Skip to main content

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.
The documentation part of this work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License . You may freely share, including copying and distributing this work in any medium or format, and freely adapt, remix, transform, and build upon the material. However, you are required to:
  • 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.