2ms Java Solution


  • 0
    S

    The idea is that the binary representation of power of 4 has all 0 except its highest bit.
    If we repeat shifting two bits left from 1, we will get all power of 4.

    public class Solution {
        public boolean isPowerOfFour(int num) {
            int mask = 1;
            while (mask <= num && mask > 0) {
                if (num == mask) return true;
                mask <<= 2;
            }
            return false;
        }
    }

Log in to reply
 

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