IncredibleThings

导航

LeetCode-Power of Four

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.

Follow up: Could you solve it without loops/recursion?
public class Solution {
    public boolean isPowerOfFour(int num) {
        if(num<=0){
            return false;
        }
        if(num==1){
            return true;
        }
        while(num%4==0){
            num=num/4;
        }
        return num==1;
    }
}

 

posted on 2016-07-19 03:13  IncredibleThings  阅读(100)  评论(0编辑  收藏  举报