342. Power of Four

    xiaoxiao2022-07-06  160

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

    Example 1:

    Input: 16

    Output: true

    Example 2:

    Input: 5

    Output: false

    Follow up: Could you solve it without loops/recursion?

     

    思路参考这里

    4的幂一定是2的幂,并且二进制中1一定在奇数位

    class Solution { public: bool isPowerOfFour(int num) { return num>0&&(num&(num-1))==0&&(num&0x55555555); } };

     

    最新回复(0)