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 > ? (n & (n-)) == : 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<=) return false; // there are multiple bits are 1
if ((num & num-) != ) 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) != ) return true;
return false;
}

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

  1. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  2. [LeetCode] 326. Power of Three + 342. Power of Four

    这两题我放在一起说是因为思路一模一样,没什么值得研究的.思路都是用对数去判断. /** * @param {number} n * @return {boolean} */ var isPowerOf ...

  3. 342. Power of Four(One-line)

    342. Power of Four     Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy Given an integer ...

  4. 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. ...

  5. Q&A in Power BI service and Power BI Desktop

    What is Q&A? Sometimes the fastest way to get an answer from your data is to ask a question usin ...

  6. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...

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

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

  8. [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:Giv ...

  9. LeetCode 342. Power of Four

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...

随机推荐

  1. 协方差Covariance的表述推导

    今天想了一下关于概率论的一维数据期望.方差以及高维数据的矩阵表示,突然想到为什么在一维中 方差的表示为:V(x) = E((x-E(x))2) 而到了高维,这样的表述就成了协方差呢?V(X) = E( ...

  2. [HRBUST1472]Coin(dp,计数)

    题目链接:http://acm-software.hrbust.edu.cn/problem.php?id=1472 题意:给n个硬币,面值随意.问恰好凑成m元的种类数(去掉重复). dp(i,j,k ...

  3. VirtualBox全屏切换

    用VirtualBox的时候,如果设置为全屏,想再切换回来,没有什么菜单,只有通过键盘的快捷键来操作,才可以恢复. 我常常忘掉,所以老是得去找,以后需要记住这几个按键的快捷键. 1.全屏与非全屏切换: ...

  4. FLASH CC 2015 CANVAS 导出音频问题

    1,导入音频无法成功发布(软件假死) 解决办法:先用个格式工厂重新压缩 在导入软件 发布 2, 音频 长度小于1秒(左右)的时候,导出后音频会变成  “哧”的一声,  估计和FLASH软件内部的音频编 ...

  5. domion Designer 管理员ID过期

    上班没几天,刚接触lotus domion 有一个服务器上打开相应的数据库提示 你的证书已经过期,网上找到的解决方案: ---------------------------------------- ...

  6. mfc 可编辑 list control

    维护到一个古老的gm工具的时候 需要这个功能 在网上找到一份很好用的代码 贴到这里 再次感谢那位同僚 #pragma once //#include "OrangeMessage.h&quo ...

  7. Yii框架第一步-- 安装

    0.首次安装请看 这里 下面的为非首次安装,不需要token的步骤 1.下载composer 官网下载: https://getcomposer.org/download/ 2.开启PHP的opens ...

  8. SQL.变量、运算符、if、while

    变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...

  9. Monkey学习(3)如何在Android模拟器中安装apk

    1.运行SDK Manager,选择模拟器,并运行模拟器,我这里用的是已经配置好的模拟器“RedMI” 2.已启动好的模拟器“RedMI” 3.记住需要安装apk文件的位置,我这里放在了F盘的根目录下 ...

  10. JavaSE复习_8 泛型程序设计

    今晚看了core Java的泛型部分,万万没有想到,当时看培训班视频入门的一带而过的泛型,有这样多的细节,整理了一下书里面提到的一些自认为的重点,方便以后观阅.由于是复习,一些基础知识跳过. △泛型类 ...