Leetcode 342. Power of Four

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

1. Description

Power of Four

2. Solution

  • Version 1
1
2
3
4
5
6
class Solution {
public:
bool isPowerOfFour(int num) {
return !(num & (num - 1)) && (num & 0x55555555);
}
};
  • Version 2
1
2
3
4
5
6
class Solution {
public:
bool isPowerOfFour(int num) {
return num > 0 && (num & (num - 1)) == 0 && (num - 1) % 3 == 0;
}
};
  • Version 3
1
2
3
4
5
6
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/
如果有收获,可以请我喝杯咖啡!