美文网首页Leetcode
Leetcode 342. Power of Four

Leetcode 342. Power of Four

作者: SnailTyan | 来源:发表于2018-09-04 21:18 被阅读1次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Power of Four

2. Solution

  • Version 1
class Solution {
public:
    bool isPowerOfFour(int num) {
        return !(num & (num - 1)) && (num & 0x55555555);
    }
};
  • Version 2
class Solution {
public:
    bool isPowerOfFour(int num) {
        return num > 0 && (num & (num - 1)) == 0 && (num - 1) % 3 == 0;
    }
};
  • Version 3
class Solution {
public:
    bool isPowerOfFour(int num) {
        return fmod(log10(num) / log10(4), 1) == 0;
    }
};

Reference

  1. https://leetcode.com/problems/power-of-four/description/

相关文章

网友评论

    本文标题:Leetcode 342. Power of Four

    本文链接:https://www.haomeiwen.com/subject/eainwftx.html