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 isPowerOfFour(int num) {
        if(num<1)return false;
        char position=-1;
        int i=0;
        while(i<=31)
        {
            if(num&(1<<(i)))
            {
                if(-1!=position&&position!=i)
                return false;
                position=i;
            }
            ++i;
        }
        if(position%2)return false;
        else return true;
    }
};

然后我发现题目中提示,你可以不用循环或者递归吗?我就去搜寻了一下这个题目发现http://www.cnblogs.com/grandyang/p/5403783.html

发现第三种方法很巧妙

时间: 2024-08-04 18:05:50

LeetCode 342. Power of Four的相关文章

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的幂. 思路

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 (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的

[LeetCode] 342. Power of Four(位操作)

传送门 Description 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的

Leetcode 342 Power of Four 数论

题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. 注意:判断一个双精度数a是否是0,请用fabs(a)<一个极小的数,如本题中用的是1e-9. 1 class Solution { 2 public: 3 bool isPowerOfFour(int num) { 4 return (num > 0 ) && (fabs((in

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 231 Power of Two(2的幂)

翻译 给定一个整型数,写一个函数来决定它是否是2的幂. 原文 Given an integer, write a function to determine if it is a power of two. 分析 详情请看这篇文章:LeetCode 326 Power of Three(3的幂)(递归.Log函数) 看题号,326是本题的加强版,326是要求不能用循环或递归的--大家可以去看看上面那篇文章. 本题就直接贴我的代码了-- 代码 class Solution { public: bo

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

【一天一道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