[LeetCode By Go 88]342. 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?

解题思路

循环整除

代码

func isPowerOfFour(num int) bool {
    if 0 >= num {
        return false
    }
    
    if 1 == num {
        return true 
    }
    for ;num > 4 && num % 4 == 0; {
        num = num / 4
    }
    
    if num % 4 == 0 {
        return true
    }
    
    return false
}
    原文作者:miltonsun
    原文地址: https://www.jianshu.com/p/0c5e6cd15168
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞