leet-code 342 4的幂判断 342. Power of Four

来源:互联网 发布:数组合并 js concat 编辑:程序博客网 时间:2024/06/05 15:02

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?

这道题我用了loops, 先把答案贴出来,然后在研究研究非loop的。

bool isPowerOfFour(int num) {    int mod = 0;   while(mod ==0 && num>= 4){       mod = num %4;       num = num/4;   }   if(mod != 0){       return false;   }   if(num == 1){       return true;   }   return false;}


0 0