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?

答案:

判断一个数是否是4的幂,不能使用循环和递归:

前面我们已经做过一个数是否为2的幂这道题,4的幂跟2的幂一样,都是最高位为1,其他位为0。

不同的是4的幂中的1在偶数位上,所以我们可以用0xaaaaaaaa与之按位与。

1 class Solution {
2 public:
3     bool isPowerOfFour(int num) {
4         return num>0&&(!(num&(num-1)))&&(!(num&(0xaaaaaaaa)));
5     }
6 };
时间: 2024-08-02 20:58:06

342. Power of Four的相关文章

231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂

231. Power of Two Given an integer, write a function to determine if it is a power of two. class Solution { public: bool isPowerOfTwo(int n) { return n > 0 ? (n & (n-1)) == 0 : false; } }; 342. Power of Four Given an integer (signed 32 bits), write

leetCode 342. Power of Four 位运算

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? 题目大意: 判断一个数是否为4的幂. 思路

342. Power of Four(One-line)

342. Power of Four Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy 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: Cou

342. Power of Four【位运算】

2017/3/23 22:23:57 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? 思路:题目要求不能循环或递归,这里采用位运

LeetCode 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? 先说明用循环: 如下 class Solution { public: bool

Python [Leetcode 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. 解题思路: 位操作. 首先判断是不是只有一位数字为1,其余为0 然后判断为1的位置是不是奇数位 代码如下: class Solution(object): def isPower

【一天一道LeetCode】#342. Power of Four

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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. F

LeetCode 342. Power of Four (4的次方)

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? 题目标签:Bit Manipulation 这道题目让我们判断一个数字是不是4的

342. Power of Four(LeetCode)

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? 1 class Solution { 2 public: 3 bool isPo