跳转至

342. Power of Four

Leetcode Bit Manipulation

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?

分析

类似于LeetCode 231. Power of Two.

public boolean isPowerOfFour(int n) {
    if (n <= 0) return false;
    while (n > 1) {
        if (n % 4 != 0) return false;
        n >>= 2;
    }
    return n == 1;
}