JAVA 1 line Solution without loops or recursion


  • 0
    public class Solution {
        public boolean isPowerOfFour(int num) {
            return num > 0 && (num & (num - 1)) == 0 && (num & 0x55555555) == num;
        }
    }
    
    1. first the num should > 0
    2. then num & num - 1 is judge whether the num is power of 2
    3. 0x55555555(signed 32 bits) is judge whether the number's 1 position is followed by even number's zero. like 100(4),10000(16)...

Log in to reply
 

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