和Power of Two类似的,我们只需要在其基础上确保其为1的位在第0,2,4...位上即可,代码如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isPowerOfFour(int num) { | |
int mask = 0x55555555; | |
return (num & mask) && !(num & (num - 1)); | |
} | |
}; |
No comments:
Post a Comment