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 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?

    bool isPowerOfFour(int num) {
        static int mask = 0b01010101010101010101010101010101;

        //edge case
        if (num<=0) return false;

        // there are multiple bits are 1
        if ((num & num-1) != 0) return false;

        // check which one bit is zero, if the place is 1 or 3 or 5 or 7 or 9...,
        // then it is the power of 4
        if ((num & mask) != 0) return true;
        return false;
    }
时间: 2024-10-07 06:47:00

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

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

LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four

位运算相关 三道题 231. Power of Two Given an integer, write a function to determine if it is a power of two. (Easy) 分析: 数字相关题有的可以考虑用位运算,例如&可以作为筛选器. 比如n & (n - 1) 可以将n的最低位1删除,所以判断n是否为2的幂,即判断(n & (n - 1) == 0) 代码: 1 class Solution { 2 public: 3 bool isP

Power BI Python 在Power BI Desktop中Python代码如何使用Power Query数据

通过Power BI Python 在Power BI Desktop中使用Python导入数据这篇文章,我们知道了Power BI如何获取Python脚本中的导入数据.那么反过来如何在Python中使用Power Query中的数据了? 首先我们进入Power Query管理器界面,通过新建一个空查询,并建立一个1到100的列表,再将其转换为表 随后我们依次点击"转换/运行Python脚本" 在脚本对话框中,输入如下所示的代码: dataset.insert(1,"add_

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

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

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的幂,不能使用循环和递归: 前面我们已经做过一