(Easy) Power of Four - LeetCode

Description:

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?

Accepted
121,084
Submissions
298,064

 

Solution:

class Solution {
    public boolean isPowerOfFour(int num) {
          int tmp = Math.abs(num);
        
        if(num<=0){
            return false;
        }
        
        if(num ==1){
            
            return true;
        }
        
      
        else{
            
            do{
                
                ; 
                int digit = tmp %4; 
                
                if(digit != 0){
                    return false;
                }
                
                tmp = tmp/4; 
                
            }
            
            while (tmp >1);
        }
        
        return true;
        
    }
}

 

posted @ 2019-08-23 10:05  CodingYM  阅读(105)  评论(0编辑  收藏  举报