Java 1-line solution, without loop or 0x55555555 hard coding


  • 0
    J
    public class Solution {
        public boolean isPowerOfFour(int num) {
            return num > 0 && Integer.bitCount(num) == 1 && Math.sqrt(num) % 1 == 0;
        }
    }
    

    The first two conditions are for checking power of two: num > 0 && Integer.bitCount(num);
    The last one is for further checking power of four: Math.sqrt(num) % 1 == 0.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.