Java 2ms 1-line Solution using Bit Manipulation


  • 3
    K

    Hints:

    1. 5 in binary is 0101
    2. number that is a power of 4 must be a power of 2
    public class Solution {
        public boolean isPowerOfFour(int num) {
            return num > 0 && (0x55555555 & num) == num && ((num - 1) & num) == 0;
        }
    }
    

  • 0
    D

    elegant solution!


Log in to reply
 

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