342. Power of Four

Problem:
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example:
Given num = 16, return true. Given num = 5, return false.
Analysis:
类似于power of three。
Solution:

class Solution {
    public boolean isPowerOfFour(int num) {
        if (num <= 0) return false;
        while (num%4 == 0) {
            num /= 4;
        }
        return num == 1;    
    }
}

评论

此博客中的热门博文

776. Split BST

663. Equal Tree Partition

532. K-diff Pairs in an Array